如何在单独的文件中声明和导入打字稿接口
How to declare and import typescript interfaces in a separate file
我想在基于 typescript 的项目中的它们自己的文件中定义几个接口,我将从中实现 类 用于生产以及用于测试的模拟。但是,我不知道正确的语法是什么。我发现了很多关于声明接口和实现它们的教程,但它们都在同一个文件中对接口和派生 类 进行了简单的实现,这不是很真实。导出和导入接口的正确方法是什么?
您需要导出定义的文件中的接口,并将它们导入到使用它们的文件中。有关示例,请参阅此 link。
x.ts
interface X{
...
}
export default X
y.ts
import X from "./x.ts"
// You can use X now
有关详细信息,请参阅 https://www.typescriptlang.org/docs/handbook/modules.html
您需要从定义的文件中导出接口,然后将其导入到任何您想要使用的地方。
在IfcSampleInterface.ts
中:
export interface IfcSampleInterface {
key: string;
value: string;
}
在SampleInterface.ts
import { IfcSampleInterface } from './IfcSampleInterface';
let sampleVar: IfcSampleInterface;
使用定义 (d.ts
) 文件和命名空间,不需要 import/export 模块这种方式。 DefinitelyTyped 项目有 guidance and huge number of examples 如何去做。
仅导出几个接口
无需传播多个 exports,您可以将它们组合在一个 export {}
块中(在这种情况下,没有文件 default
应声明类型):
// interfaces.ts
interface IWords {
[key: string]: string;
}
interface INumbers {
[key: string]: number;
}
interface IBooleans {
[key: string]: boolean;
}
interface IValues {
[key: string]: string | number;
}
interface IStructures {
[key: string]: INumbers | IBooleans | IValues;
}
export {
// not exporting IWords | INumbers
IBooleans,
IValues,
IStructures,
}
导入示例
import { IBooleans, IValues, IStructures } from 'interfaces';
const flags: IBooleans = { read: true, write: false, delete: false };
const userFile: IValues = { user: 1, username: 'One', file: 'types.txt' };
const userContext: IStructure = {
file: userFile,
permissions: flags,
counts: { views: 3, writes: 1 } // => INumbers (lint: try to remove IValues from IStructures)
};
在比较新的项目中可以使用如下语法
`import type { xxx } from './xxx'`
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html
我想在基于 typescript 的项目中的它们自己的文件中定义几个接口,我将从中实现 类 用于生产以及用于测试的模拟。但是,我不知道正确的语法是什么。我发现了很多关于声明接口和实现它们的教程,但它们都在同一个文件中对接口和派生 类 进行了简单的实现,这不是很真实。导出和导入接口的正确方法是什么?
您需要导出定义的文件中的接口,并将它们导入到使用它们的文件中。有关示例,请参阅此 link。
x.ts
interface X{
...
}
export default X
y.ts
import X from "./x.ts"
// You can use X now
有关详细信息,请参阅 https://www.typescriptlang.org/docs/handbook/modules.html
您需要从定义的文件中导出接口,然后将其导入到任何您想要使用的地方。
在IfcSampleInterface.ts
中:
export interface IfcSampleInterface {
key: string;
value: string;
}
在SampleInterface.ts
import { IfcSampleInterface } from './IfcSampleInterface';
let sampleVar: IfcSampleInterface;
使用定义 (d.ts
) 文件和命名空间,不需要 import/export 模块这种方式。 DefinitelyTyped 项目有 guidance and huge number of examples 如何去做。
仅导出几个接口
无需传播多个 exports,您可以将它们组合在一个 export {}
块中(在这种情况下,没有文件 default
应声明类型):
// interfaces.ts
interface IWords {
[key: string]: string;
}
interface INumbers {
[key: string]: number;
}
interface IBooleans {
[key: string]: boolean;
}
interface IValues {
[key: string]: string | number;
}
interface IStructures {
[key: string]: INumbers | IBooleans | IValues;
}
export {
// not exporting IWords | INumbers
IBooleans,
IValues,
IStructures,
}
导入示例
import { IBooleans, IValues, IStructures } from 'interfaces';
const flags: IBooleans = { read: true, write: false, delete: false };
const userFile: IValues = { user: 1, username: 'One', file: 'types.txt' };
const userContext: IStructure = {
file: userFile,
permissions: flags,
counts: { views: 3, writes: 1 } // => INumbers (lint: try to remove IValues from IStructures)
};
在比较新的项目中可以使用如下语法
`import type { xxx } from './xxx'`
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html