TypeScript Blueprints by Ivo Gabe de Wolff
Author:Ivo Gabe de Wolff [Wolff, Ivo Gabe de]
Language: eng
Format: azw3
Publisher: Packt Publishing
Published: 2016-07-28T04:00:00+00:00
Assignments
Previously, the compiler did not follow assignments of a variable. If a variable was reassigned in the block after an if statement, the narrowing would not be applied. Thus, in the next example, the type of x is string | number, both before and after the assignment:
let x: string | number = ...; if (typeof x === "string") { x = 4; }
With control flow based type analysis, these assignments can be checked. The type of x will be string before the assignment and number after it. Narrowing after an assignment works only for union types. The parts of the union type are filtered based on the assigned value. For types other than union types, the type of the variable will be reset to the initial type after an assignment.
This can be used to write a function that either accepts one value or a list of values, in one of the following ways:
function f(x: string | string[]) { if (typeof x === "string") x = [x]; // x: string[] } function g(x: string | string[]) { if (x instanceof Array) { for (const item of x) g(item); return; } // x: string }
With the same analysis, the compiler can also check for values that are possibly null or undefined. Instead of getting runtime errors saying undefined is not an object, you will get a compile time warning that a variable might be undefined or null.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(26419)
Hello! Python by Anthony Briggs(25330)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(24574)
Kotlin in Action by Dmitry Jemerov(23657)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(22991)
Dependency Injection in .NET by Mark Seemann(22776)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21525)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20378)
Grails in Action by Glen Smith Peter Ledbrook(19433)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17056)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16429)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14118)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12303)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11574)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10645)
Hit Refresh by Satya Nadella(9223)
The Kubernetes Operator Framework Book by Michael Dame(8579)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8432)
Robo-Advisor with Python by Aki Ranin(8376)