简体   繁体   English

从 JavaScript 扩展 TypeScript 接口:TypeError: Class extends value undefined is not a function or null

[英]Extending a TypeScript interface from JavaScript: TypeError: Class extends value undefined is not a function or null

I've a TypeScript interface like this:我有一个这样的 TypeScript 界面:

export interface Foo {
  bar: string
}

This will compile down to an empty .js file.这将编译为一个空的.js文件。 So when using the TypeScript interface in an ES6 Node module, I'll get this error:所以当在 ES6 Node 模块中使用 TypeScript 接口时,我会得到这个错误:

TypeError: Class extends value undefined is not a function or null

A workaround will be to create a class一种解决方法是创建一个类

export class BaseFoo {
  bar: string
}

Now I can use BaseFoo from ES6 without getting the aforementioned error.现在我可以使用 ES6 中的BaseFoo而不会出现上述错误。 So it is not possible to just have interfaces in TypeScript with ES6 interop or is there an more elegant way than having Base* classes (which are not really base classes in the end) here?所以不可能只在 TypeScript 中使用带有 ES6 互操作的接口,或者有比在此处使用Base*类(最终不是真正的基类)更优雅的方式吗?

Update: I understand the different concepts behind interfaces and base classes but my questions is about a best practice how to create a library in TypeScript which uses interfaces and how to build it in a way plain ES6 (which does not have interfaces) can also consume this library.更新:我了解接口和基类背后的不同概念,但我的问题是关于如何在 TypeScript 中创建使用接口的库以及如何以普通 ES6(没有接口)也可以使用的方式构建它的最佳实践这个图书馆。

I think you my be misunderstanding how to properly use an interface. 我认为您误会了如何正确使用接口。 Interfaces are not "Base" objects, they simple define what an object should look like. 接口不是“基础”对象,它们可以简单地定义对象的外观。

In typescript, many times an interface is used as a type for an object, but in general can be used to define what a class should "look like" as well. 在打字稿中,通常将接口用作对象的类型,但通常也可以用来定义类的“外观”。

For instance: 例如:

interface Foo {
  bar: string;
  baz: number;
  bang(): void;
}

In this case, any object/class that implements Foo will need to have these three things. 在这种情况下, implements Foo任何对象/类都需要具备这三样东西。

IE: IE:

class FooImplemented implements Foo {
  bar = 'Hello World';
  baz = 42;

  bang() {
    print(this.bar);
  }
}

If I do not define bar, baz and/or bang; 如果我未定义bar,baz和/或bang; typescript will throw an error at compile time that I'm not properly implementing the Foo interface in the FooImplemented class. Typescript将在编译时引发错误,因为我没有在FooImplemented类中正确实现Foo接口。

To summarize, an interface tells the compiler what something is supposed to look like. 总而言之,一个接口告诉编译器应该是什么样子。 And an interface can extend another interface. 一个接口可以扩展另一个接口。 However, a class cannot extend an interface, but it can implement one. 但是,类不能扩展接口,但是可以实现一个接口。 (Or multiple) (或多个)

Check out the docs on interfaces on typescripts website. 在打字稿网站上查看界面上的文档。

You may want to use this @implements jsdoc annotation :您可能想要使用这个@implements jsdoc 注释

/** @implements {Print} */
class TextBook {
  print() {
    // TODO
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:类扩展值未定义不是函数或 null - TypeError: Class extends value undefined is not a function or null TypeError: Class extends value undefined is not a constructor 或 null - TypeError: Class extends value undefined is not a constructor or null 'Uncaught TypeError: Class extends value undefined is not a constructor or null' 尝试加载外部 TypeScript 组件时 - 'Uncaught TypeError: Class extends value undefined is not a constructor or null' when trying to load external TypeScript component React Class 中的“TypeError: Class extends value undefined is not a constructor or null”问题 - Issue with “TypeError: Class extends value undefined is not a constructor or null” in React Class 反应TypeError:类扩展未定义的值不是构造函数或null - React TypeError: Class extends value undefined is not a constructor or null Nodemailer:未捕获类型错误:Class 扩展值未定义不是构造函数或 null - Nodemailer: Uncaught TypeError: Class extends value undefined is not a constructor or null REACT TypeError: Class extends value undefined is not a constructor or null - REACT TypeError: Class extends value undefined is not a constructor or null 类型错误:Class extends value undefined is not a constructor or null in react js - TypeError: Class extends value undefined is not a constructor or null in react js TypeError: Class extends value undefined is not a constructor 或 null (svelte redis) - TypeError: Class extends value undefined is not a constructor or null (svelte redis) Reactjs TypeError:类扩展值未定义不是构造函数或空值 - Reactjs TypeError: Class extends value undefined is not a constructor or null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM