Questions tagged [typescript2.0]

Tag for questions specific to new features in TypeScript 2.x. For general TypeScript questions, the correct tag is TypeScript.

Tag for questions specific to new features in TypeScript 2.x. For general TypeScript questions, the correct tag is .

TypeScript 2.0 documentation

1412 questions
52
votes
5 answers

TypeScript character type?

My question is very brief. I am new to TypeScript, been searching around here and there but didn't find yet an answer. Does any experienced TypeScripter know if there is a character Type or an easy way to achieve one?
charliebrownie
  • 4,591
  • 8
  • 28
  • 49
46
votes
7 answers

How to merge two enums in TypeScript

Suppose I have two enums as described below in Typescript, then How do I merge them enum Mammals { Humans, Bats, Dolphins } enum Reptiles { Snakes, Alligators, Lizards } export default Mammals & Reptiles // For Illustration…
besrabasant
  • 1,323
  • 1
  • 19
  • 33
45
votes
5 answers

TypeScript - what type is f.e. setInterval

If I'd like to assign a type to a variable that will later be assigned a setInterval like so: this.autoSaveInterval = setInterval(function(){ if(this.car.id){ this.save(); } else{ this.create(); } …
gfels
  • 1,929
  • 4
  • 21
  • 44
45
votes
3 answers

'this' is undefined inside the foreach loop

I am writing some typescript code and iterating an array. Inside the loop, I am trying to access 'this' object to do some processing as: console.log('before iterate, this = ' +this); myarray.days.forEach(function(obj, index) { …
user1892775
  • 1,533
  • 2
  • 28
  • 41
41
votes
4 answers

How to rewrite the protected/private route using TypeScript and React-Router 4, 5 or 6?

I was trying to create a as describe in the react-router documents using TypeScript. Can anyone help me out? The privateRoute in react-router document: const PrivateRoute = ({ component: Component, ...rest }) => (
Charlie
  • 1,520
  • 1
  • 12
  • 25
40
votes
3 answers

After upgrading TypeScript, Angular controller registration now fails to compile

We were using TypeScript 2.2. After upgrading to 2.4, we now get this on compilation: error TS2345: Argument of type 'typeof TopMenuController' is not assignable to parameter of type 'Injectable'. Type 'typeof…
user47589
37
votes
3 answers

Is there an equivalent to "sealed" or "final" in TypeScript?

I'm trying to implement a method in a super class that should be available for use, but not changeable, in sub classes. Consider this: export abstract class BaseClass { universalBehavior(): void { doStuff(); // Do some universal stuff…
bubbleking
  • 2,655
  • 3
  • 21
  • 41
32
votes
8 answers

Does TypeScript have a Null-conditional operator

Is there any operator like ?. in TypeScript that can check if the variable is null or not defined like Kotlin? Like person?.getName()?.firstName ?: "None"
Da Yin
  • 323
  • 1
  • 3
  • 6
32
votes
2 answers

Property is not assignable to string index in interface

I have the following interfaces: export interface Meta { counter: number; limit: number; offset: number; total: number; } export interface Api { [key: string]: T[]; meta: Meta; // error } Currently, I'm receiving the following…
dev_054
  • 2,568
  • 6
  • 21
  • 47
31
votes
1 answer

Saving npm @types typings with --save or --save-dev

TypeScript 2 recommends using npm for types. In The Future of Declaration Files. the example is: npm install --save @types/lodash My question is whether --save-dev should be used in an application, because TypeScript is transpiled and not deployed?…
30
votes
6 answers

TypeScript check for empty string

Is there a way for TypeScript to statically check for an empty string? Is there a way to statically require a non-empty string to be passed to a function? let fn = function(a:string){ }; fn(''); or let a = ''; fn(a); Can TS help us here?
Alexander Mills
  • 1
  • 80
  • 344
  • 642
30
votes
4 answers

Operator '==' cannot be applied to types x and y in Typescript 2

TypeScript Version: 2.0.2.0 Code I know the code is a bit stupid, but I actually have these kind of tests in my code (making an expression visitor) and I really think these should fly and compile right away. var a: boolean = (true == false); var…
Lostfields
  • 1,028
  • 1
  • 10
  • 20
28
votes
3 answers

Typescript: Is there a simple way to convert an array of objects of one type to another

So, I have two classes Item { name: string; desc: string; meta: string} ViewItem { name: string; desc: string; hidden: boolean; } I have an array of Item that needs to be converted into an array of ViewItem. Currently, I am looping through the…
Shilpa Nagavara
  • 885
  • 2
  • 12
  • 29
27
votes
4 answers

Typescript error This condition will always return 'true' since the types have no overlap

I having this condition on a form group: if((age>17 && (this.frType=="Infant")) || (age>40 && this.frType=="Grandchild") || (age<=5 && (this.frType!="Child" || this.frType!="Infant" || this.frType!="Grandchild" ||…
alim1990
  • 3,312
  • 9
  • 49
  • 91
27
votes
3 answers

What is the TypeScript equivalent of React.PropTypes.node?

I have searched high and low, and I can find TypeScript equivalents for everything except React's PropTypes.node. I know that TypeScript doesn't need any PropTypes, but there are some PropTypes that I don't know how to convert to TypeScript. Is it…
Magician
  • 1,434
  • 3
  • 21
  • 35
1
2
3
94 95