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

2019前端面试题 —— typescript相关 #24

Open
fengmiaosen opened this issue Sep 5, 2019 · 1 comment
Open

2019前端面试题 —— typescript相关 #24

fengmiaosen opened this issue Sep 5, 2019 · 1 comment

Comments

@fengmiaosen
Copy link
Owner

fengmiaosen commented Sep 5, 2019

  1. 泛型的作用

    泛型

  2. unknown类型

    unknown 类型不能赋值给除了 unknown 或 any 的其他任何类型,使用前必需显式进行指定类型,或是在有条件判断情况下能够隐式地进行类型推断的情况

    新的unknown 类型,它是 any 类型对应的安全类型。
    unknown 和 any 的主要区别是 unknown 类型会更加严格:在对 unknown 类型的值执行大多数操作之前,我们必须进行某种形式的检查。而在对 any 类型的值执行操作之前,我们不必进行任何检查

  3. interface 和 type 到底有什么区别

    参考官方文档

    • 相同点
    1. 都可以描述一个对象或者函数
    2. 都允许拓展(interface extends)但语法不同,type是通过联合来实现扩展
    • 不同点

    type 可以声明基本类型别名,联合类型,元组等类型,interface不可以
    type 还可以使用 typeof 获取实例的 类型进行赋值
    interface 能够声明合并,type不可以

  4. 高级类型

    https://www.tslang.cn/docs/handbook/advanced-types.html

  5. TypeScript 的高级类型
    https://juejin.im/post/5eb6d9cde51d454de777297d?utm_source=gold_browser_extension

参考资料

@bhaltair
Copy link

bhaltair commented Jul 19, 2020

type 类型别名能被 extends和 implements

// extends
type Parent = { x: number; }
interface Child extends Parent { y: number }

// implements
type Point2 = {
  x: number;
  y: number;
}
class SomePoint2 implements Point2 {
  x = 1
}

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

No branches or pull requests

2 participants