Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript问题记录 #4

Open
wangxingkang opened this issue Feb 10, 2020 · 2 comments
Open

TypeScript问题记录 #4

wangxingkang opened this issue Feb 10, 2020 · 2 comments

Comments

@wangxingkang
Copy link
Owner

wangxingkang commented Feb 10, 2020

如何定义指定Key的对象类型

export type T = 
  'key1' | 
  'key2' |
  'key3';

export type Bar = {
  [key in T]: string;
};

相关参考

@wangxingkang
Copy link
Owner Author

wangxingkang commented Feb 27, 2020

?. 的使用

TypeScript 3.7版本才可使用

使用场景当不确定值为 null 或 undefined 的时候,可以使用 ?.

例如:

let x = foo?.bar.baz();

// 相当于
let x = (foo === null || foo === undefined) 
   ? undefined 
   : foo.bar.baz();

@wangxingkang
Copy link
Owner Author

wangxingkang commented Feb 27, 2020

?? 的使用

TypeScript 3.7版本才可使用

使用场景: 要给一个值默认值的时候可使用,主要替换 let a = b || 'aa';

例如:

let x = foo ?? bar();

let x = (foo !== null && foo !== undefined)
  ? foo 
  : bar();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant