简体   繁体   English

如何在声明文件(* .d.ts)中导入第三方模块?

[英]How do I import third-party module in declaration file(*.d.ts)?

I created the type declaration file( index.d.ts ), and I need to use an object of the third-party package('moment.js' in node_modules ) as type. 我创建的类型声明文件( index.d.ts ),我需要使用第三方包的对象(在“moment.js” node_modules )类型。


  // index.d.ts

  import * as Moment from 'moment';

  declare var $: any;
  declare var google: any;

  interface foo {
    a: Moment;
    b: string;
    ...
  } 

I made a code like above, but it doesn't work. 我做了上面的代码,但是没有用。 How do I import a third-party module in *.d.ts file? 如何在*.d.ts文件中导入第三方模块?

The .d.ts file that ships with Moment wraps wraps everything it exports into a namespace. Moment包装随附.d.ts文件将其导出的所有内容包装到命名空间中。 So for this lib, importing like import * as Moment from 'moment'; 因此,对于这个库, import * as Moment from 'moment';import * as Moment from 'moment';一样import * as Moment from 'moment'; means the Moment variable is a namespace not the interface for a Moment instance itself. 表示Moment变量是一个名称空间,而不是Moment实例本身的接口。 What you want is the interface that lives inside the namespace. 您需要的是位于名称空间内的接口。

You have two options to handle this: 您有两种选择来处理此问题:

  import * as moment from 'moment';

  interface foo {
    a: moment.Moment;
    b: string;
  } 

Or deconstruct the interface during the import: 或在导入过程中解构接口:

  import { Moment } from 'moment';

  interface foo {
    a: Moment;
    b: string;
  } 

Personally, I use the first style for files of mine that use lots exported members from the namespace, but if I only need a few I use the second style. 就我个人而言,我使用的是我的文件的第一种样式,该文件使用了从命名空间导出的大量成员,但是如果我只需要少数文件,则使用第二种样式。

edit... 编辑...

If your tsconfig has esModuleInterop enabled (which is the newish preferred setting), the import from first example can/should remove the * as bit. 如果您的tsconfig启用了esModuleInterop (这是最新的首选设置),则从第一个示例导入可以/应该将* as位删除。

import moment from 'moment';

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

相关问题 如何在没有d.ts文件的情况下导入第三方软件包? - How can I import third party package without d.ts file? 如何从`.d.ts`文件导入? - How do I import from a `.d.ts` file? 如何导入一个 js 模块。 (没有任何声明文件(d.ts)) - How to import a js Module as any. (Without any declaration file (d.ts)) d.ts 声明模块在导入第三方库时不起作用 - d.ts declare module not work when import third party libs 如何在Angular中使用自定义的Typescript声明文件(.d.ts)? - How do I use a custom Typescript Declaration file (.d.ts) from within Angular? 在 TypeScript 声明中导入 JSON 文件(*.d.ts 文件) - Import JSON file in TypeScript declaration (*.d.ts files) 如何引用NPM包中tsc的“声明”选项生成的TypeScript .d.ts文件中声明的类型? - How do I reference a type declared in a TypeScript .d.ts file generated by tsc's “declaration” option in NPM package? 如何在 Typescript 中实现声明 .d.ts 文件 - How to implement declaration .d.ts file in Typescript 如何为全局第三方库导入 TypeScript 模块定义 - How to import a TypeScript module definition for a global third-party library 如何在不将其转换为模块的情况下在`d.ts` 文件中导入类型(从另一个文件)? - How to import a type (from another file) inside a `d.ts` file without turning it into a module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM