TypeScript - difference between import … and import {…} (with curly braces)

后端 未结 3 1155
有刺的猬
有刺的猬 2020-12-13 12:38

Coming from Java to TS, I\'ve omitted the {...} around the imported type.

import DiscriminatorMappingData from \'./DiscriminatorMappingData\';
         


        
3条回答
  •  借酒劲吻你
    2020-12-13 13:17

    The difference between your two import declarations is covered in the TypeScript specification. From §11.3.2, Import Declarations:

    An import declaration of the form

    import d from "mod";

    is exactly equivalent to the import declaration

    import { default as d } from "mod";

    Thus, you would omit the braces only when you are importing something that was exported as the default entity of the module (with an export default declaration, of which there can only be one per module). The name you provide in the import declaration becomes an alias for that imported entity.

    When importing anything else, even if it's just one entity, you need to provide the braces.

    The Default exports section of the TypeScript handbook has a few examples.

提交回复
热议问题