
我使用 vscode 编辑器
在使用 js 时:
const path = require("path"); 悬浮在 path 上,编辑器是能正常检测到 path 的类型:
(alias) namespace path (alias) const path: PlatformPath import path 但使用 ts 时,相同的代码,编辑器把 path 识别为 any
我猜测是 ts 的问题,我的版本是 4.7.4
如何让 ts 检查 require 的类型?
ps:因为是 nodejs 环境,所以只使用 require 而不是 import
2 rbe 2022 年 7 月 11 日 用 import path = require('path') 语法 https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require |
4 xushengfeng OP @br_wang 有 |
5 xushengfeng OP @rbe import 在 nodejs 上报错 |
6 br_wang 2022 年 7 月 11 日 @xushengfeng node 版本在 v13 以上可以在 package.json 里声明 {"type": "module"} 来支持 import 语法。 |
7 rbe 2022 年 7 月 11 日 @xushengfeng #5 你不是说你在用 ts 吗,设置 tsconfig.json 的 compilerOptions.module 为 commonjs ,编译完 import 就是 require 了 |
8 br_wang 2022 年 7 月 11 日 |
9 xushengfeng OP @rbe 确实编译成 require 了但是多了 Object.defineProperty(exports, "__esModule", { value: true }); exports 会让其报错,去掉后一切正常,怎样配置才能让他不输出这行呢 |