TypeScript subtype All In One
TypeScript subtype All In One
subtype / assignment
any | unknown | object | void | undefined | null | never | |
---|---|---|---|---|---|---|---|
any → | ✓ | ✓ | ✓ | ✓ | ✓ | ❌ | |
unknown → | ✓ | ❌ | ❌ | ❌ | ❌ | ❌ | |
object → | ✓ | ✓ | ❌ | ❌ | ❌ | ❌ | |
void → | ✓ | ✓ | ❌ | ❌ | ❌ | ❌ | |
undefined → | ✓ | ✓ | ✅ | ✓ | ✅ | ❌ | |
null → | ✓ | ✓ | ✅ | ✅ | ✅ | ❌ | |
never → | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
https://www.typescriptlang.org/docs/handbook/type-compatibility.html#subtype-vs-assignment
demo
// export {};
let n: null = null;
let u: undefined = undefined;
// let test: boolean = null;
// Type 'null' is not assignable to type 'boolean'.(2322)
// let test: boolean = undefined;
// Type 'undefined' is not assignable to type 'boolean'.(2322)
// let test: object = Object;
// let test: object = Array;
// let test: object = Number;
// let test: object = String;
// let test: object = {};
// let test: object = [];
// let test: object = null;
// Type 'null' is not assignable to type 'object'.(2322)
// let test: object = undefined;
// Type 'undefined' is not assignable to type 'object'.(2322)
// let test: unknown = undefined;
// let test: unknown = null;
// let test: any = undefined;
// let test: any = null;
// let test: void = undefined;
// let test: void = null;
// Type 'null' is not assignable to type 'void'.(2322)
// let test: undefined = undefined;
// let test: undefined = null;
// Type 'null' is not assignable to type 'undefined'.(2322)
// let test: null = null;
// let test: null = undefined;
// Type 'undefined' is not assignable to type 'null'.(2322)
// let test: never = undefined;
// Type 'undefined' is not assignable to type 'never'.(2322)
// let test: never = null;
// Type 'null' is not assignable to type 'never'.(2322)
// let test: never = '';
// Type 'string' is not assignable to type 'never'.(2322)
let test: never;
console.log('test =', test);
// Variable 'test' is used before being assigned.(2454)
// let test = 'abc';
// console.log('test =', test);
// 模块声明
export {};
// export {};
let n: null = null;
let u: undefined = undefined;
let test: boolean = null;
// Type 'null' is not assignable to type 'boolean'.(2322)
let test2: boolean = undefined;
// Type 'undefined' is not assignable to type 'boolean'.(2322)
console.log('test =', test);
console.log('test2 =', test2);
// let test = 'abc';
// console.log('test =', test);
// 模块声明
export {};
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15862361.html
未经授权禁止转载,违者必究!