How to declare and import typescript interfaces in a separate file

前端 未结 4 1429
长发绾君心
长发绾君心 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:52

    You need to export the interfaces in the file the are defined in and import them in the files they are used in. See this link for examples.

    x.ts

    interface X{
        ...
    }
    export default X
    

    y.ts

    import X from "./x.ts"
    // You can use X now
    

    For more information see https://www.typescriptlang.org/docs/handbook/modules.html

提交回复
热议问题