xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-02-04 01:24  xgqfrms  阅读(35)  评论(2编辑  收藏  举报