typescript 报错 TS1005 ; expected

今天遇到这个报错,

error TS1005: '=' expected. import type { keyType } from './aa';

如下图所示:

package.json 中 编译 ts 的命令如下 "build-ts": "tsc -p tsconfig.build.json"

这个报错 主要是因为使用了下面这种语法

import type { keyType } from './someType';

这个语法 import type仅仅导入, 这是 TypeScript 3.8 新增的属性

关于 import type 想了解更多 可以看这俩:

https://segmentfault.com/a/1190000039800522
https://www.typescriptlang.org/docs/handbook/modules.html#importing-types

找到原因是版本问题,那就按照网上的教程 npm install typecript@4.4.4 -g 安装了最新版本

但是还报错,后来在node_modules 下看到有个 typescript 一看版本是 3.7.2, 原来这个项目 本身是安装了 typescript的,

赶紧把该项目的typescript 更新到最新版本,问题解决, 如下图所示

总结:

这里其实涉及到了 node_modules 模块查找的问题, 当使用 tsc -p tsconfig.build.json 时, 首先会在 目录下的 node_modules 下中查找typsscript模块,
没有的话 再往上级,一直找到全局环境, 所以之前就是 先使用了 该项目中的 typescrip,导致了报错,虽然typescript 全局安装的版本是没问题, 但是 根本没使用到全局版本

原文地址:https://www.cnblogs.com/yalong/p/15502222.html