代码之家  ›  专栏  ›  技术社区  ›  Falieson

typescript导入/导出对象和展开

  •  2
  • Falieson  · 技术社区  · 7 年前

    /// file1
    export {
      PORT,
      SSL_CRT,
      SSL_KEY,
    }
    
    // file2
    import * as env from 'file1'
    // env.PORT is available at this point
    export {
       ...env  // [ts] Identifier expected. [1003] 
    }
    

    我用以下方法解决这个问题,但我知道它不一样,也不知道我想要什么。

    // file2
    import * as env from 'file1'
    export default {
       ...env  // [ts] Identifier expected. [1003] 
    }
    
    2 回复  |  直到 7 年前
        1
  •  5
  •   unional    7 年前

    您试图做的是重新导出。以下是如何:

    // file2.ts
    export * from './file1'
    

    import * as env .

        2
  •  0
  •   Brian Jenkins    7 年前

    // file1.ts
    export {
        PORT,
        SSL_CRT,
        SSL_KEY
    };
    
    // file2.ts
    import * as env from "./file1";
    
    console.log(env); // { PORT: 'foo', SSL_CRT: 'bar', SSL_KEY: 'baz' }
    
    export { env };
    
    // file3.ts
    import { env } from "./file2";
    
    console.log(env); // { PORT: 'foo', SSL_CRT: 'bar', SSL_KEY: 'baz' }
    

    因此,为了回答您的问题:您不需要使用spread操作符,因为对象正在隐式地被解构。为什么会发生这种情况,是一个比我聪明的人的问题。

    推荐文章