TypeScript import with extension

こ雲淡風輕ζ 提交于 2021-01-03 06:38:04

问题


You may have heard of Deno which is a new TypeScript runtime.

One major difference between Deno and normal TypeScript is that you must include the file extension in the import statement. e.g:

import foo from './bar.ts'
                       ^^

I would like to write code that is compatible with both Deno and Webpack.

How can I configure Webpack to allow importing with .ts extension like above?

Also, how can I prevent the following VSCode error?


回答1:


Webpack can be configured to resolve the extensions of all imports with the resolve property. If there is an empty string within the list of extensions webpack will accept imports with full extension as well. The empty string should be the first entry in the list.

module.exports = {
 // ...
 resolve: {
   extensions: ['', '.ts', '.tsx' /*etc ...*/],
 }
}

If there is no empty string in the list of extensions to use webpack will try to import something like ./bar.ts.ts instead of ./bar.ts.

You can disable warnings in VSCode from the ts-compiler using a comment like

// @ts-ignore TS6133
import foo from './bar.ts'


来源:https://stackoverflow.com/questions/56199426/typescript-import-with-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!