How to update TypeScript to latest version with npm?
Try npm install -g typescript@latest. You can also use npm update instead of install, without the latest modifier.
Try npm install -g typescript@latest. You can also use npm update instead of install, without the latest modifier.
Bit old, but doesn’t hurt to add some notes. When you write something like this let a: any; let b: Object; let c: {}; a has no interface, it can be anything, the compiler knows nothing about its members so no type checking is performed when accessing/assigning both to it and its members. Basically, you’re … Read more
It turns out it’s possible to specify the type after : for the whole destructuring pattern: const {foo}: {foo: IFoo[]} = bar; Which in reality is not any better than plain old const foo: IFoo[] = bar.foo;
This is because of the combination of two things: tsconfig not having any files section. From http://www.typescriptlang.org/docs/handbook/tsconfig-json.html If no “files” property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories. When a “files” property is specified, only those files are included. Including typescript as an npm … Read more
You can use a type predicate function in the .filter to avoid opting out of strict type checking: function notEmpty<TValue>(value: TValue | null | undefined): value is TValue { return value !== null && value !== undefined; } const array: (string | null)[] = [‘foo’, ‘bar’, null, ‘zoo’, null]; const filteredArray: string[] = array.filter(notEmpty); Alternatively … Read more
Update April 2017: As of version 2.13.0, Moment includes a typescript definition file. https://momentjs.com/docs/#/use-it/typescript/ Just install it with npm, in your console type npm install –save moment And then in your Angular app, import is as easy as this: import * as moment from ‘moment’; That’s it, you get full Typescript support! Bonus edit: To … Read more
See the reference for user-defined type guard functions for more information. function isString(test: any): test is string{ return typeof test === “string”; } function example(foo: any){ if(isString(foo)){ console.log(“it is a string” + foo); console.log(foo.length); // string function } } example(“hello world”); Using the type predicate test is string in the above format (instead of just … Read more
It wasn’t possible before but luckily it is now, since TypeScript version 2.1. It has been released on the 7th of December 2016 and it introduces indexed access types also called lookup types. The syntax looks exactly like element access but written in place of types. So in your case: interface I1 { x: any; … Read more
Can I tell the interface to default the properties I don’t supply to null? What would let me do this No. You cannot provide default values for interfaces or type aliases as they are compile time only and default values need runtime support Alternative But values that are not specified default to undefined in JavaScript … Read more
By injecting an instance of ActivatedRoute one can subscribe to a variety of observables, including a queryParams and a params observable: import {Router, ActivatedRoute, Params} from ‘@angular/router’; import {OnInit, Component} from ‘@angular/core’; @Component({…}) export class MyComponent implements OnInit { constructor(private activatedRoute: ActivatedRoute) {} ngOnInit() { // Note: Below ‘queryParams’ can be replaced with ‘params’ depending … Read more