Shorthand 对于 TS 接口上的“任何”

Shorthand for `any` on a TS interface

我不小心得到了这个,它似乎有效:

export interface ITestBlockOpts {
  desc: string,
  title: string,
  opts: Object,
  suman: ISuman,
  gracefulExit,
  handleBeforesAndAfters, 
  notifyParent
}

对于行:

 gracefulExit,
  handleBeforesAndAfters, 
  notifyParent

的语法缩写
  gracefulExit: any
  handleBeforesAndAfters: any
  notifyParent: any

?

是的。 TypeScript 编译器有一个选项可以假设未类型化的变量应该被键入为 any。这是 the noImplicitAny option,默认情况下是 false

它的存在主要是为了更容易从 JavaScript 转换(其中 TypeScript 是一个严格的超集,即当 noImplicitAny == false 时所有有效的 JavaAscript 都是有效的 TypeScript)。

如果您在新建项目或全 TypeScript 项目中使用 TypeScript,那么您应该使用 noImplicitAny == true 以确保您始终使用类型化变量和字段。

请注意,在 TypeScript 中,如果接口成员具有 any 类型(无论是否隐式),则它与可选 (undefined) 成员不同。

...所以这些接口 等效的:

interface Foo {
    a: any           // explicit `any` type
}
interface Bar {
    a                // implicit 'any' type
}

...但是这些接口并不等同:

interface Foo {
    a: any
}
interface Baz {
    a?: any          // explicit `any` type, but `a` can also be `undefined`
}
interface Qux {
    a?               // implicit `any`, but `a` can also be `undefined`
}