简体   繁体   English

使用JS中的TypeScript类型

[英]Using TypeScript types from JS

I've enabled checkJS and allowJS in Typescript and created a simple fooInterface.d.ts file: 我在Typescript中启用了checkJSallowJS并创建了一个简单的fooInterface.d.ts文件:

declare interface FooInterface {
  x();
} 

I'm trying to typecheck a JS file against this interface: 我正在尝试对此接口进行JS文件的类型检查:

/**
 * @implements {FooInterface}
 */
class Bar {
  y() {}
}

However I can't get it to throw an error upon running tsc . 但是我无法在运行tsc时抛出错误。

Also tried simply writing declare class , didn't work either. 也尝试过简单地编写declare class ,也不起作用。

How can I reuse types from .d.ts files in JSDoc+JS? 如何重用JSDoc + JS中.d.ts文件中的类型?

Supporting the JSDoc @implements tag has been requested in TypeScript and is tracked by https://github.com/Microsoft/TypeScript/issues/17498 在TypeScript中已经要求支持JSDoc @implements标记,并且可以通过https://github.com/Microsoft/TypeScript/issues/17498进行跟踪。

The @extends tag will be supported in the next release of TypeScript (2.7) and is already available in the nightly builds (typescript@next) which, I might add, are remarkably stable. @extends标记将在下一版本的TypeScript(2.7)中得到支持,并且在夜间构建(typescript @ next)中已经可用,我可以补充一下,该构建非常稳定。

You can get the nightly build with your package manager of choice. 您可以通过选择的包管理器进行每晚构建。 For example 例如

using npm 使用npm

$ npm install typescript@next

using jspm 使用jspm

$ jspm install typescript@next

using yarn 用纱

$ yarn add typescript@next

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM