Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 类型的类型脚本问题_Typescript_Typescript Typings - Fatal编程技术网

Typescript 类型的类型脚本问题

Typescript 类型的类型脚本问题,typescript,typescript-typings,Typescript,Typescript Typings,这段代码怎么可能编译,它应该失败,并且在第一个if之后抛出类型 class Optional<T> {} class Some<T> extends Optional<T> { constructor(public t: T) { super() } } function div(n: number, d: number): Optional<number> { if (d === 0.0) { return "

这段代码怎么可能编译,它应该失败,并且在第一个if之后抛出类型

class Optional<T> {}

class Some<T> extends Optional<T> {
  constructor(public t: T) {
      super()
  }
}

function div(n: number, d: number): Optional<number> {
  if (d === 0.0) {
    return "IMPOSSIBLE"
  }
  return new Some(d / n)
}

console.log(div(1, 0))
类可选{}
类的一些扩展是可选的{
构造函数(公共t:t){
超级()
}
}
函数div(n:number,d:number):可选{
如果(d==0.0){
返回“不可能”
}
返回新的部分(d/n)
}
控制台日志(div(1,0))
因为TypeScript有一个属性,这意味着它不是通过名称而是通过属性来判断类型兼容性,所以空类本质上是
任何
,但对于少数类型,如
null
未定义
,由于它不需要任何属性

因为TypeScript有一个属性,这意味着它不是通过名称而是通过属性来判断类型兼容性,所以空类本质上是
任何
,但对于少数类型,如
null
未定义
,因为它不需要任何属性