【问题标题】:Define class in Typescript module在 Typescript 模块中定义类
【发布时间】:2021-10-27 04:44:17
【问题描述】:

我正在为外部库编写一个 Typescript 模块。

这是我index.d.ts的一部分

declare module 'my-external-library' {
  export class MyComponent extends React.Component<MyComponentProps> {}

  export class CustomClass {
    constructor(
     firstParam: string,
     secondParam: string,
    )
  }

  interface MyComponentProps {
    config: string
    customClass: CustomClass
    text?: string
    customFn?: (input: string) => boolean
  }

}

除了customClass 属性外,这很好用。 自定义类是一个必须是这样的类:

class customClass {
  constructor(firstParam, secondParam) {
    this.firstParam = firstParam
    this.secondParam = secondParam
  }

  ... all the methods you want
}

export default customClass

在一个单独的文件中,我将customClass 属性赋予MyComponent,如下所示:


import customClass from './customClass'

<MyComponent
    config: "test"
    customClass: customClass
/>

customClass 实际上没有接收到index.d.ts 中声明的“构造函数类型”,但组件的所有其他属性都运行良好。 那么,我所做的类定义是正确的吗?我必须用另一种方法导出类?

这是一个 CodeSandbox:https://codesandbox.io/s/react-typescript-playground-forked-nrfk2

感谢您的帮助,请原谅我的经验不足

【问题讨论】:

  • 我不确定我是否正确理解了这个问题,但也许您想扩展 CustomClass?否则customClass实现与CustomClass类型定义没有关系?类 customClass 扩展 CustomClass { ... }
  • 可能只是问题中的一个错误,但您的类型定义适用于CustomClass,而您的js代码适用于customClass(JavaScript IS区分大小写)跨度>
  • @RobertDempsey 我明白你在说什么。我的想法是存在这种关系是因为我将道具“customClass”赋予组件MyComponent。类型检查适用于其他道具,为什么不适用于类?是否必须扩展类以便继承类型?
  • 感谢您花时间进行设置。恐怕我还是不太明白这个问题。具体来说,我正在查看ActionProvider.ts。这 是 您定义 ActionProvider 类的地方,并且 ActionProvider 和您的 index.d.ts 文件之间没有链接,因此它不能继承您根本没有引用的东西。如果您希望您的类推断其变量的类型,您需要执行 class ActionProvider extends SomeOtherClass 或 class ActionProvider implements SomeInterface
  • 出于好奇,您是否尝试过 MyComponentProps 定义中的customClass: typeof CustomClass?如果您想将类本身作为值传递,则类型需要是构造函数的类型而不是实例的类型。

标签: javascript typescript types


【解决方案1】:

我已经分叉并编辑了您的代码沙箱,请查看:

现在我要解释一些关于打字稿中的类的事情,这确实很混乱。

部分原因是人们使用术语“类”来指代差异上下文中的差异事物。为了消除歧义,让我们使用更准确的术语。

构造函数

MyPuppy 通常被称为“类”,但在 JS 术语中它也是“构造函数”。因此,让我们在整个讨论中坚持使用 constructor。

// javascript
class MyPuppy {}

实例

变量puppy 是MyPuppet 的实例

// javascript
const puppy = new MyPuppet()

实例类型与构造函数类型

在 typescript 中,当您声明如下所示的类时,您将隐式声明一个同名的实例类型!

这种行为让很多人感到困惑。简而言之,MyPuppy 作为 JS 变量 持有 constructor,而 MyPuppy 作为 TS 类型变量 实际上持有 实例类型。

现在我们如何引用MyPuppy 构造函数的类型,也就是构造函数类型?你应该直接使用typeof MyPuppy。

// typescript
class MyPuppet {}

type MyPuppetInstance = MyPuppet
type MyPuppetConstructor = typeof MyPuppet

我们已经消除了歧义,让我们做一些案例研究。

曾经在old TS handbook 中记录了类的“实例端”和“静态端”的概念。基本上“实例端”对应于实例类型,“静态端”对应于构造函数类型。

class MyPuppy {
  static getSpecies(dog: MyPuppy) {
    return dog.species
  }

  constructor(species: string) { this.species = species }

  species: string

  // [side note] above two lines can be combined as one:
  // constructor(public species: string) {}

  poop() { return '?' }
}

type MyPuppetInstance = MyPuppet
type MyPuppetConstructor = typeof MyPuppet


var bar: MyPuppetConstructor = MyPuppy
var foo: MyPuppetInstance = new bar("shiba_inu")

上面的 sn-p 做了几件事。

  1. 声明一个名称为MyPuppy的JS构造函数。
  2. 隐式声明一个 TS 实例类型 MyPuppy。
  3. 在“实例端”,foo 是一个实例,并且有一个属性species 和一个方法poop。
  4. 在“静态方面”,bar 是一个构造函数,它是一个可以作为new bar("shiba_inu") 调用的“新函数”,并且还有一个静态方法getSpecies。

现在在.d.ts 文件中,我们如何声明上述类型?您可以使用interface 关键字或type 关键字。它们几乎没有区别。

// index.d.ts

interface MyPuppyInstance {
  species: string
  poop(): string
}

interface MyPuppyConstructor {
  new (species: string): MyPuppyInstance
  getSpecies(dog: { species: string }): string
}

type MyPuppyInstance2 = {
  species: string
  poop(): string
}

type MyPuppyConstructor2 = 
  (new (species: string) => MyPuppyInstance)
  & { getSpecies(dog: { species: string }): string }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-10
    • 2017-02-17
    • 1970-01-01
    • 2016-08-30
    • 2017-03-06
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    相关资源
    最近更新 更多