3 ms·
Typescript allows this const c = getCustomer(); // type is Customer | null const z : Customer = c; // ERROR; incompatible types if (x === null) {
by edflsafoiewq 6y ago
Typescript allows this
const c = getCustomer(); // type is Customer | null
const z : Customer = c; // ERROR; incompatible types
if (x === null) {
// handle null
} else {
const y : Customer = c; // OK; knows c isn't null here
}
- throw_m239339 6y agoWell Typescript is really a static code analyser for Javascript and not a language, since it does nothing at runtime, and that's fine, its type system can catch a broad range of issues and improve code quality drastically. It's a huge godsend for big codebases. But afaik it doesn't guard against null by default. A simpler example: declare type Foo = {name:String}; declare function getFoo():Foo|null; const f = getFoo(); if(f!==null) // mandatory or yields an error console.log(f.name); But afaik it doesn't guard against null by default.