gpt4 book ai didi

typescript - 为什么 `export type` 需要 `--isolatedModules` ?

转载 作者:行者123 更新时间:2023-12-04 10:02:58 30 4
gpt4 key购买 nike

我不明白发生了什么,也不明白为什么要这样做,在引擎盖下:

  • Something是一种类型,为什么export { SomeThing } from "elsewhere" --isolatedModules 时产生错误是否启用?

    相反为什么是export { SomeThing } from "elsewhere"如果 Something 不是错误是一个值,或者如果 --isolatedModules未启用?
  • 为什么指定 export type { SomeThing } from "elsewhere"修复那个错误?


  • (背景资料)

    a new TypeScript feature :

    Type-Only Imports and Export

    This feature is something most users may never have to think about; however, if you’ve hit issues under --isolatedModules, TypeScript’s transpileModule API, or Babel, this feature might be relevant.

    TypeScript 3.8 adds a new syntax for type-only imports and exports.

    import type { SomeThing } from "./some-module.js";

    export type { SomeThing };

    import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there’s no remnant of it at runtime. Similarly, export type only provides an export that can be used for type contexts, and is also erased from TypeScript’s output.



    我知道如何使用它以及何时需要,但我不知道为什么,即显然何时 --isolatedModules启用然后代码如下...

    import type { SomeThing } from "./some-module.js";

    export { SomeThing };

    ... 产生编译器错误,即 ...
    Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.ts(1205)

    ... 修复方法是使用 export type { SomeThing }而不是 export { SomeThing } .

    顺便说一句,显然 import { SomeThing } from "./some-module.js"没问题,不产生错误信息, import type { SomeThing } from "./some-module.js"不需要。

    顺便说一下,这与 What is `export type` in Typescript? 不是同一主题这是关于定义 type并添加前缀 export ,在 2017 年实现此新功能之前。

    最佳答案

    this Babel Github issue , 解释如下:

    In order to determine whether something is a type, we need informationabout other modules.

    import { T } from "./other-module";
    export { T }; // Is this a type export?

    this Reddit thread 上也有很好的详细解释:

    type space is TypeScript's domain: all of the types that make up yourapp. They only exist at compile time. Once the compiler is done, theresulting JavaScript has no types left in it. Value space is basicallythe opposite: it's the stuff that sticks around and survives into theJS.

    type Foo = { bar: string };
    const a: Foo = { bar: 'hello' };

    Here, Foo is in the type space,everything else is in the value space.

    The problem with isolatedModules, is they can't know which space theyare working with, since each file is compiled individually. So if youdo:

    export { Foo } from './bar';

    in a file, TypeScript can't know if Foois in the type space (if it is, it just gets thrown away as it won'tbe in the resulting JavaScript), or if it is in the value space (ieit's JS, so it needs to be included in the resulting output).

    关于typescript - 为什么 `export type` 需要 `--isolatedModules` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61744429/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com