How to declare and import typescript interfaces in a separate file

前端 未结 4 1430
长发绾君心
长发绾君心 2020-12-12 20:14

I want to define several interfaces in their own file in my typescript-based project, from which I\'ll implement classes for production as well as mocks for testing. However

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-12 20:59

    You need to export the interface from the file in which is defined and import it wherever you want to use it.

    in IfcSampleInterface.ts:

    export interface IfcSampleInterface {
       key: string;
       value: string;
    }
    

    In SampleInterface.ts

    import { IfcSampleInterface } from './IfcSampleInterface';
    let sampleVar: IfcSampleInterface;
    

提交回复
热议问题