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
17,045
questions
138
votes
10
answers
50k
views
How do I return the response from an Observable/http/async call in angular?
I have service which returns an observable which does an http request to my server and gets the data. I want to use this data but I always end up getting undefined. What's the problem?
Service:
@...
186
votes
6
answers
58k
views
Transform union type to intersection type
Is there a way to transform a union type into an intersection type :
type FunctionUnion = (() => void) | ((p: string) => void)
type FunctionIntersection = (() => void) & ((p: string) =>...
46
votes
4
answers
15k
views
How to define Typescript type as a dictionary of strings but with one numeric "id" property
My existing JavaScript code has "records" where the id is numeric and the other attributes are strings. I'm trying to define this type:
type T = {
id: number;
[key: string]: string
}
...
227
votes
16
answers
195k
views
How can I use/create dynamic template to compile dynamic Component with Angular 2.0?
I want to dynamically create a template. This should be used to build a ComponentType at runtime and place (even replace) it somewhere inside of the hosting Component.
Until RC4 I was using ...
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 ...
38
votes
2
answers
8k
views
Difference between Variance, Covariance, Contravariance, Bivariance and Invariance in TypeScript
Could you please explain using small and simple TypeScript examples what is Variance, Covariance, Contravariance, Bivariance and Invariance?
744
votes
15
answers
992k
views
How can I select an element in a component template?
Does anybody know how to get hold of an element defined in a component template? Polymer makes it really easy with the $ and $$.
I was just wondering how to go about it in Angular.
Take the example ...
146
votes
9
answers
78k
views
Equivalent of $compile in Angular 2
I want to manually compile some HTML containing directives. What is the equivalent of $compile in Angular 2?
For example, in Angular 1, I could dynamically compile a fragment of HTML and append it ...
690
votes
21
answers
767k
views
access key and value of object using *ngFor
I am a bit confused about how to get the key and value of an object in angular2 while using *ngFor for iterating over the object. I know in angular 1.x there is a syntax like
ng-repeat="(key, value) ...
213
votes
7
answers
79k
views
Why doesn't Object.keys return a keyof type in TypeScript?
Title says it all - why doesn't Object.keys(x) in TypeScript return the type Array<keyof typeof x>? That's what Object.keys does, so it seems like an obvious oversight on the part of the ...
571
votes
10
answers
570k
views
Use async await with Array.map
Given the following code:
var arr = [1,2,3,4,5];
var results: number[] = await arr.map(async (item): Promise<number> => {
await callAsynchronousOperation(item);
return item +...
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 ...
646
votes
29
answers
882k
views
How do I cast a JSON Object to a TypeScript class?
I read a JSON object from a remote REST server. This JSON object has all the properties of a typescript class (by design). How do I cast that received JSON object to a type var?
I don't want to ...
144
votes
14
answers
81k
views
Typescript: deep keyof of a nested object
So I would like to find a way to have all the keys of a nested object.
I have a generic type that take a type in parameter. My goal is to get all the keys of the given type.
The following code work ...
67
votes
6
answers
29k
views
How to transform union type to tuple type
For example, I have a type:
type abc = 'a' | 'b' | 'c';
How to make a tuple type that contains all elements of the union at compile time?
type t = ['a','b', 'c'];