ts 引入作为配置项的 config.js 文件时如何解决类型问题?

2023-03-15 13:55:34 +08:00
 wdssmq
import config from '../config.js'
src/main.ts:3:20 - error TS7016: Could not find a declaration file for module '../config.js'. 'path/config.js' implicitly has an 'any' type.
707 次点击
所在节点    问与答
6 条回复
sechi
2023-03-15 14:00:05 +08:00
把 config.js 改成 config.ts 呗
hua123s
2023-03-15 14:04:07 +08:00
写一个 config.d.ts 呗,不然就 ts-ignore
wdssmq
2023-03-15 15:57:22 +08:00
AI 给了条命令,姑且备忘下 - -

tsc --allowJs --declaration --outDir ./typings config.js
hua123s
2023-03-15 16:24:56 +08:00
@wdssmq 我不如 AI 😭😭😭
wdssmq
2023-03-15 16:55:33 +08:00
@hua123s 其实,我也没说 AI 的命令有用 - -

现在变成了 config.default 才能拿到需要的对象,然后并没有定义到正确的类型。Orz
wdssmq
2023-03-15 17:05:54 +08:00
tsconfig 配置了 "allowJs": true, 好像可以了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/924186

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX