exportdefaultinterfaceConfig{name:string}// export default type Config1 = {// name: string// }// 会报错typeConfig2= {name:string}exportdefaultConfig2 总结 interface 和 type 很像,很多场景,两者都能使用。但也有细微的差别: 类型:对象、函数两者都适用,但是 type 可以用于基础类型、联合类型、元祖。
export default 在ES6 模块系统中,使用export default可以导出一个默认值,使用方可以用import foo from 'foo'而不是import { foo } from 'foo'来导入这个默认值。 同样,在类型声明文件中,我们可以通过export default用来导出默认值的类型。比如: image.png 需要额外注意的是只有function、class和interface可以直接默认...
export default interface Config { name: string } // export default type Config1 = { // name: string // } // 会报错 type Config2 = { name: string } export default Config2 复制代码 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 总结 interface 和 type 很像,很多场景...
import { MyType } from "./MyType";declare global { interface Function { applyParams?(aa: string[]): MyType; }}function f() {}const a: Function = f;a.applyParams && a.applyParams(["1", "2"]); 这可能是因为TypeScript处理包含和不包含导入语句的文件的方式不同。具有导入的文件被视为...
interface 用于定义接口。 let 定义块级作用域的变量。 module 定义模块(在较早的 TypeScript 版本中使用)。 namespace 定义命名空间(在较早的 TypeScript 版本中使用)。 new 创建类的实例。 null 表示空值。 number 表示数字类型。 object 表示非原始类型。 of 用于for...of 循环。 package 用于模块系统,标识...
}// 导出接口exportinterfaceMyInterface{name:string;greet():void; } 默认导出 除了命名导出外,TypeScript还支持默认导出(default exports)。每个模块只能有一个默认导出,它可以是任何类型的成员。使用export default语法进行默认导出: // 默认导出类classDefaultClass{constructor(publicname:string) { ...
相同点 1.都可以描述一个对象或者函数 interface interface People { name: string age: number } interface setPeople { (name:string,age:number):void } type type People = {
export default instance // 定义结果 接口的 result // unknown 未知类型 export interface Result<T = unknown> { message: string; code: number; data: T; [key: string]: any; // 其他任意类型 } // 请求结果的类型 Result /* 定义一个名为Result的接口 默认类型为 泛型T unknown 未知类型 ...
\n\n比如,我们在自己定义的 axios.d.ts 中:\n\nts\n// types/axios.d.ts\n\ndeclare function axios(): string;\n\n// 此时声明的 interface 为模块内部的String声明\ndeclare interface String {\n hello: () => void;\n}\n\nexport default axios;\n\n// index.ts\n'a'.hello() // ...
export default Lottie; //src/type.d.ts import Lottie from 'lottie-web'; declare interface Window { lottie: Lottie; } 但是,TS 编译器认为 Lottie 是一个值不是类型。 TS2749: 'Lottie' refers to a value, but is being used as a type here....