Skip to content

第 15 章 “import type 语句” 小节,关于报错的描述有疑问,求指教。 #96

Closed
@AnglusWang

Description

@AnglusWang

tsconfig.json 配置

{
    "compilerOptions": {
        "target": "ES2020",
        "module": "ES2020",
        "outDir": "dist",
        "strict": true,
    },
    "include": ["src/**/*.ts"],
    "exclude": ["node_modules"]
}

原文描述

第二个方法是使用 import type 语句,这个语句只能输入类型,不能输入正常接口。

// 正确
import type { A } from './a';

// 报错
import type { a } from './a';

上面示例中,import type 输入类型A是正确的,但是输入正常接口a就会报错。

实测

正常引入第二个语句 import type { a } from './a'; 时不会报错, 作为类型使用时也能正常使用,作为非类型的值使用时才会报错。例如:

import type { a } from './2'
type C = typeof a //正常

import type { a } from './2'
let c = a //报错: "a" 是使用 "import type" 导入的,因此不能用作值。ts(1361)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions