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
25
votes
1 answer

Narrowing down error type in catch

For this piece of code try { throw new CustomError(); } catch (err) { console.log(err.aPropThatDoesNotExistInCustomError); } err is any and doesn't trigger type errors. How can it be narrowed down to the type that error is expected to be?
Estus Flask
  • 150,909
  • 47
  • 291
  • 441
23
votes
4 answers

TypeScript interface with XOR, {bar:string} xor {can:number}

How do I say that I want an interface to be one or the other, but not both or neither? interface IFoo { bar: string /*^XOR^*/ can: number; }
A T
  • 10,508
  • 14
  • 85
  • 137
23
votes
4 answers

Can you declare a object literal type that allows unknown properties in typescript?

Essentially I want to ensure that an object argument contains all of the required properties, but can contain any other properties it wants. For example: function foo(bar: { baz: number }) : number { return bar.baz; } foo({ baz: 1, other: 2…
Lucas
  • 11,997
  • 7
  • 62
  • 114
23
votes
3 answers

StrictNullChecks and getElementById

With StrictNullChecks enabled in TypeScript 2.0, the code var el: HTMLElement = document.getElementById('element_id'); produces the error Type 'HTMLElement | null' is not assignable to type 'HTMLElement'. Type 'null' is not assignable to type…
skoll
  • 1,862
  • 3
  • 26
  • 31
23
votes
2 answers

How do I compile my TypeScript code for Node.js to one file?

I want to compile TypeScript to one file when using with Node.js. I have tried configuring "tsconfig.json" like this: "compilerOptions": { "module": "commonjs", "target": "es6", "noImplicitAny": false, "sourceMap": false, …
user236582
  • 347
  • 1
  • 2
  • 6
22
votes
4 answers

document.getElementById("id") may be null

Edit: I'm using TypeScript v2.2.1 I am new to TypeScript and I'm not sure what the cleanest way of dealing with DOM elements that may or may not exist is. Basically, I want to check whether an element exists, and then if it does, add an event…
Evan Hefner
  • 327
  • 1
  • 2
  • 5
21
votes
5 answers

I want totally immutable object in TS

I have some big object, like const a={ b:33, c:[78, 99], d:{e:{f:{g:true, h:{boom:'selecta'}}}};/// well, even deeper than this... And I'd like TS not to allow me to do a.d.e.f.h.boom='respek'; How can I immutate the object completely? Is it…
shal
  • 2,110
  • 3
  • 15
  • 25
21
votes
3 answers

Angular 1.x with TypeScript 2.x, @types, and SystemJS - Using global typings

I am trying to use Angular 1.x with the new TS2 and @types registry but running into problems. It looks like @types does not use what the typings registry refered to as "global" typings. I am running into the following error: error TS2686: 'angular'…
Kerry Ritter
  • 921
  • 4
  • 14
  • 25
20
votes
3 answers

experimental support for decorators visual studio 2017

I have created a .Net Core project with AngularJS in Visual Studio 2017 however when I am trying to create a service, I am getting an error as Experimental support for decorators is a feature that is subject to change in a future release. Set the…
LearningPal
  • 540
  • 2
  • 8
  • 25
20
votes
3 answers

Typescript : underscore convention for members

I have a class Email class Email { private _from: string; private _to: Array; private _subject: string; } It'll create an email object something like: { _from:'', _to:'', _subject:'' } This seems a little weird to me since I…
19
votes
3 answers

How to use TypeScript with withRouter, connect, React.Component and custom properties?

I have a React Component that uses connect, withRouter and receives custom properties. I am trying to convert this to TypeScript and I'm wondering if I'm doing this correctly. At least, I have no errors, now. This is the code that shows the…
chmielot
  • 359
  • 1
  • 3
  • 8
19
votes
3 answers

How to get tsc to Resolve Absolute Paths when Importing Modules using baseUrl?

Consider a simple typescript project with the following directory structure: | package.json | tsconfig.json | \---src | app.ts | \---foobar Foo.ts Bar.ts tsconfig.json has been configured…
Zsw
  • 3,312
  • 3
  • 21
  • 41
19
votes
5 answers

Import module from root path in TypeScript

Let's suppose I've a project, and its main source directory is: C:\product\src Based on this directory, every import path would be relative to it. I.e., suppose: // Current script: C:\product\src\com\name\product\blah.ts import { thing } from…
user7536774
18
votes
1 answer

No provider for InjectionToken angularfire2.app.options

Recently I started to learn to use the concept of firebase in combination with angular. As a start, I try to make the login process work. Currently, I get an ennoying error when I try to navigate to the login page and I cannot figure out what is…
Cornelis
  • 1,287
  • 5
  • 16
  • 36
18
votes
2 answers

Left-hand side of assignment expression cannot be a constant or a read-only property

When I use this line on my Express server, it works well in TypeScript 1.x mongoose.Promise = global.Promise; ( The usage of mongoose.Promise = global.Promise; is from the mongoose document ) After updating to TypeScript 2.x, it shows this error in…
Hongbo Miao
  • 31,551
  • 46
  • 124
  • 206
1 2
3
94 95