前言
本文使用的Typescript版本为5.5.2
配置
{
compilerOptions: {
"allowImportingTsExtensions": true
}
}
说明
allowImportingTsExtensions是typescript@5.x后加入的字段。
配置启用后,import
其他模块时允许携带.ts
, .mts
和 .tsx等typescript支持的扩展名
。
// 默认的引入方式,不需要加ts后缀名
import {a as a1} from './demo2'
import {a as a2} from './demo2.ts'
import {a as a3} from './demo1.tsx'
import {a as a4} from './demo3.mts'
import {a as a5} from './demo4.cts'
export {a1, a2, a3, a4, a5}
不开启的时候会报错如下:
ts/index.ts:4:23 - error TS5097: An import path can only end with a '.ts' extension when 'allow