Questions tagged [typescript]
TypeScript is a typed superset of JavaScript that transpiles to plain JavaScript. It adds optional types to JavaScript. This tag is for questions specific to TypeScript. It is not used for general JavaScript questions.
typescript
232,710
questions
2612
votes
24
answers
940k
views
Interfaces vs Types in TypeScript
What is the difference between these statements (interface vs type) in TypeScript?
interface X {
a: number
b: string
}
type X = {
a: number
b: string
};
1927
votes
5
answers
672k
views
What is TypeScript and why would I use it in place of JavaScript? [closed]
What is the TypeScript language? What can it do that JavaScript or available libraries cannot do, that would give me reason to consider it?
1648
votes
21
answers
2.6m
views
How to convert a string to number in TypeScript?
Given a string representation of a number, how can I convert it to number type in TypeScript?
var numberString: string = "1234";
var numberValue: number = /* what should I do with `numberString`? */;
1366
votes
7
answers
572k
views
In TypeScript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
When looking at the source code for a tslint rule, I came across the following statement:
if (node.parent!.kind === ts.SyntaxKind.ObjectLiteralExpression) {
return;
}
Notice the ! operator after ...
1351
votes
52
answers
1.5m
views
Can't bind to 'formGroup' since it isn't a known property of 'form'
The situation
I am trying to make what should be a very simple form in my Angular application, but no matter what, it never works.
The Angular version
Angular 2.0.0 RC5
The error
Can't bind to '...
1276
votes
32
answers
833k
views
How do you explicitly set a new property on `window` in TypeScript?
I setup global namespaces for my objects by explicitly setting a property on window.
window.MyNamespace = window.MyNamespace || {};
TypeScript underlines MyNamespace and complains that:
The ...
1076
votes
40
answers
1.7m
views
Could not find a declaration file for module 'module-name'. '/path/to/module-name.js' implicitly has an 'any' type
I read how TypeScript module resolution works.
I have the following repository: @ts-stack/di.
After compiling the directory structure is as follows:
├── dist
│ ├── annotations.d.ts
│ ├── ...
966
votes
40
answers
1.3m
views
Property '...' has no initializer and is not definitely assigned in the constructor
in my Angular app i have a component:
import { MakeService } from './../../services/make.service';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-vehicle-form',
...
945
votes
12
answers
1.0m
views
get and set in TypeScript
I'm trying to create get and set method for a property:
private _name: string;
Name() {
get:
{
return this._name;
}
set:
{
this._name = ???;
}
}
What's the ...
869
votes
30
answers
1.0m
views
How do I dynamically assign properties to an object in TypeScript?
If I wanted to programmatically assign a property to an object in Javascript, I would do it like this:
var obj = {};
obj.prop = "value";
But in TypeScript, this generates an error:
The ...
860
votes
21
answers
1.3m
views
What is "not assignable to parameter of type never" error in TypeScript?
Code is:
const foo = (foo: string) => {
const result = []
result.push(foo)
}
I get the following TS error:
[ts] Argument of type 'string' is not assignable to parameter of type 'never'.
What ...
846
votes
5
answers
463k
views
When to use JSX.Element vs ReactNode vs ReactElement?
I am currently migrating a React application to TypeScript. So far, this works pretty well, but I have a problem with the return types of my render functions, specifically in my functional components.
...
825
votes
9
answers
590k
views
Are strongly-typed functions as parameters possible in TypeScript?
In TypeScript, I can declare a parameter of a function as a type Function. Is there a "type-safe" way of doing this that I am missing? For example, consider this:
class Foo {
save(callback: ...
821
votes
23
answers
1.0m
views
Element implicitly has an 'any' type because expression of type 'string' can't be used to index
Trying out TypeScript for a React project and I'm stuck on this error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ train_1: boolean; ...
807
votes
11
answers
300k
views
'unknown' vs. 'any'
TypeScript 3.0 introduces unknown type, according to their wiki:
unknown is now a reserved type name, as it is now a built-in type.
Depending on your intended use of unknown, you may want to ...