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.

Filter by
Sorted by
Tagged with
2552 votes
24 answers
897k 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 };
user avatar
2127 votes
51 answers
1.7m views

Can't bind to 'ngModel' since it isn't a known property of 'input'

I have this simple input in my component which uses [(ngModel)] : <input type="text" [(ngModel)]="test" placeholder="foo" /> And I get the following error when I ...
Anthony Brenelière's user avatar
1923 votes
5 answers
666k 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?
Mohammed Thabet's user avatar
1623 votes
20 answers
2.5m 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`? */;
Paul0515's user avatar
  • 24.6k
1341 votes
7 answers
547k 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 ...
Mike Chamberlain's user avatar
1338 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 '...
FrancescoMussi's user avatar
1257 votes
30 answers
809k 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 ...
joshuapoehls's user avatar
  • 34.1k
1034 votes
40 answers
1.6m 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 │   ├── ...
ktretyak's user avatar
  • 29.7k
944 votes
37 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', ...
Mikhail Kostiuchenko's user avatar
943 votes
12 answers
993k 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 ...
MuriloKunze's user avatar
  • 15.4k
861 votes
30 answers
979k views

How do I dynamically assign properties to an object in TypeScript?

If I wanted to programatically 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 property '...
Peter Olson's user avatar
844 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 ...
Lev's user avatar
  • 14.9k
822 votes
9 answers
579k 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: ...
vcsjones's user avatar
  • 140k
819 votes
5 answers
436k 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. ...
Golo Roden's user avatar
  • 146k
777 votes
23 answers
982k 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; ...
DLee's user avatar
  • 8,716
776 votes
11 answers
281k 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 ...
Jac Mos's user avatar
  • 8,376
768 votes
10 answers
437k views

About "*.d.ts" in TypeScript

I am curious about .d.ts declaration files. I was told by someone that .d.ts files are similar to .h header files in the C & C++ programming languages, however, the .d.ts files don't seem to work ...
user avatar
763 votes
31 answers
743k views

How do I convert a string to enum in TypeScript?

I have defined the following enum in TypeScript: enum Color{ Red, Green } Now in my function I receive color as a string. I have tried the following code: var green= "Green"; var color : Color =...
Amitabh's user avatar
  • 60.1k
763 votes
20 answers
1.3m views

How do I remove an array item in TypeScript?

I have an array that I've created in TypeScript and it has a property that I use as a key. If I have that key, how can I remove an item from it?
Tim Almond's user avatar
  • 12.4k
750 votes
36 answers
1.5m views

Unable to resolve dependency tree error when installing npm packages

When trying to install the npm packages using npm i command, I am getting the following exception: I have tried reinstalling the Node.js package and setting the proxy to off using: set HTTP_PROXY= ...
Pearl's user avatar
  • 8,899
742 votes
15 answers
977k 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 ...
Aman Gupta's user avatar
  • 8,043
741 votes
28 answers
624k views

What is the type of the 'children' prop?

I have a very simple functional component as follows: import * as React from 'react'; export interface AuxProps { children: React.ReactNode } const aux = (props: AuxProps) => props....
Asool's user avatar
  • 13.7k
740 votes
28 answers
957k views

No provider for HttpClient

After upgrading from angular 4.4 to 5.0 and after updating all HttpModule and Http to HttpClientModule I started to get this error. I also added HttpModule again to be sure it's not due to some ...
Himmet Yelekin's user avatar
739 votes
28 answers
1.2m views

Is there a way to check for both `null` and `undefined`?

Since TypeScript is strongly-typed, simply using if () {} to check for null and undefined doesn't sound right. Does TypeScript have any dedicated function or syntax sugar for this?
David Liu's user avatar
  • 17.1k
739 votes
20 answers
958k views

How to run TypeScript files from command line?

I'm having a surprisingly hard time finding an answer to this. With plain Node.JS, you can run any js file with node path/to/file.js, with CoffeeScript it's coffee hello.coffee and ES6 has babel-node ...
Gunchars's user avatar
  • 10.3k
734 votes
17 answers
428k views

What is the syntax for Typescript arrow functions with generics?

The typescript handbook currently has nothing on arrow functions. Normal functions can be generically typed with this syntax: example: function identity<T>(arg: T): T { return arg; } What ...
Andreas Frische's user avatar
731 votes
7 answers
739k views

Possible to extend types in Typescript?

Say I have the following type: type Event = { name: string; dateCreated: string; type: string; } I now want to extend this type, i.e. type UserEvent extends Event = { UserId: string; } ...
Kousha's user avatar
  • 34.8k
706 votes
9 answers
785k views

Enforcing the type of the indexed members of a Typescript object?

I would like to store a mapping of string -> string in a Typescript object, and enforce that all of the values map to strings. For example: var stuff = {}; stuff["a"] = "foo"; ...
Armentage's user avatar
  • 12.5k
699 votes
44 answers
971k views

How to get names of enum entries?

I would like to iterate a TypeScript enum object and get each enumerated symbol name, for example: enum myEnum { entry1, entry2 } for (var entry in myEnum) { // use entry's name here, e.g., "...
CalvinDale's user avatar
  • 9,393
693 votes
13 answers
715k views

How to define type for a function callback (as any function type, not universal any) used in a method parameter

Currently I have type definition as: interface Param { title: string; callback: any; } I need something like: interface Param { title: string; callback: function; } but the 2nd one ...
Smrutiranjan Sahu's user avatar
688 votes
21 answers
756k 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) ...
Pardeep Jain's user avatar
  • 85.7k
673 votes
30 answers
853k views

Interface type check with Typescript

This question is the direct analogon to Class type check in TypeScript I need to find out at runtime if a variable of type any implements an interface. Here's my code: interface A { member: string;...
lhk's user avatar
  • 28.7k
670 votes
10 answers
543k views

How to implement class constants?

In TypeScript, the const keyword cannot be used to declare class properties. Doing so causes the compiler to an error with "A class member cannot have the 'const' keyword." I find myself in ...
BeetleJuice's user avatar
  • 40.3k
645 votes
29 answers
867k 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 ...
David Thielen's user avatar
641 votes
24 answers
842k views

TypeScript getting error TS2304: cannot find name ' require'

I am trying to get my first TypeScript and DefinitelyTyped Node.js application up and running, and running into some errors. I am getting the error "TS2304: Cannot find name 'require' " when I ...
JerryKur's user avatar
  • 7,409
622 votes
15 answers
456k views

Does Typescript support the ?. operator? (And, what's it called?)

Does Typescript currently (or are there plans to) support the safe navigation operator of ?. ie: var thing = foo?.bar // same as: var thing = (foo) ? foo.bar : null; Also, is there a more common ...
Marty Pitt's user avatar
  • 29.1k
610 votes
4 answers
537k views

What is the Record type?

What does Record<K, T> mean in Typescript? Typescript 2.1 introduced the Record type, describing it in an example: // For every properties K of type T, transform it to U function mapObject<K ...
Matthias's user avatar
  • 14.6k
605 votes
18 answers
428k views

Constructor overload in TypeScript

Has anybody done constructor overloading in TypeScript. On page 64 of the language specification (v 0.8), there are statements describing constructor overloads, but there wasn't any sample code given. ...
Ted's user avatar
  • 6,151
590 votes
10 answers
1.2m views

How do I check whether an array contains a string in TypeScript?

Currently I am using Angular 2.0. I have an array as follows: var channelArray: Array<string> = ['one', 'two', 'three']; How can I check in TypeScript whether the channelArray contains a ...
code1's user avatar
  • 8,721
589 votes
15 answers
688k views

Type definition in object literal in TypeScript

In TypeScript classes it's possible to declare types for properties, for example: class className { property: string; }; How do declare the type of a property in an object literal? I've tried the ...
Dace Zarina's user avatar
  • 6,672
588 votes
9 answers
362k views

The difference between "require(x)" and "import x"

I've just started working on a small node project that will interface with a MongoDB. However, I cannot seem to get the relevant node modules to import correctly, even though I have installed them ...
austinthemassive's user avatar
581 votes
11 answers
851k views

How to use `@ts-ignore` for a block?

The // @ts-ignore comment enables the TypeScript compiler to ignore the line below it. How can one ignore a whole block of code with TypeScript?
vadersfather's user avatar
  • 8,709
575 votes
20 answers
1.1m views

Typescript: Type 'string | undefined' is not assignable to type 'string'

When I make any property of an interface optional, and while assigning its member to some other variable like this: interface Person { name?: string, age?: string, gender?: string, occupation?:...
asdasd's user avatar
  • 6,720
573 votes
16 answers
748k views

How can I create an object based on an interface file definition in TypeScript?

I have defined an interface like this: interface IModal { content: string; form: string; href: string; $form: JQuery; $message: JQuery; $modal: JQuery; $submits: JQuery; }...
user avatar
564 votes
10 answers
233k views

An index signature parameter type cannot be a union type. Consider using a mapped object type instead

I'm trying to use the following pattern: enum Option { ONE = 'one', TWO = 'two', THREE = 'three' } interface OptionRequirement { someBool: boolean; someString: string; } interface ...
john maccarthy's user avatar
560 votes
11 answers
370k views

Can't bind to 'formControl' since it isn't a known property of 'input' - Angular2 Material Autocomplete issue

I am trying to use Angular Material Autocomplete component in my Angular 2 project. I added the following to my template. <md-input-container> <input mdInput placeholder="Category" [...
Lahiru Chandima's user avatar
558 votes
12 answers
167k views

@Directive vs @Component in Angular

What is the difference between @Component and @Directive in Angular? Both of them seem to do the same task and have the same attributes. What are the use cases and when to prefer one over another?
Prasanjit Dey's user avatar
553 votes
10 answers
549k 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 +...
Alon's user avatar
  • 11.2k
543 votes
31 answers
950k views

How to suppress "error TS2533: Object is possibly 'null' or 'undefined'"?

I have a type: type tSelectProtected = { handleSelector?: string, data?: tSelectDataItem[], wrapperEle?: HTMLElement, inputEle?: HTMLElement, listEle?: HTMLElement, resultEle?: ...
user avatar
542 votes
10 answers
235k views

What is the difference between types String and string?

Does anyone know the difference between String and string in TypeScript? Am I correct in assuming that they ought to be the same? var a: String = "test"; var b: string = "another test&...
Paul0515's user avatar
  • 24.6k

1
2 3 4 5
4610