如何在Heroku上执行ts文件时声明typescript类型?



我有一个typescript文件,我使用ts-node在本地执行:

"scripts": {
"commands": "ts-node --files deploy-commands.ts",
},

当我使用Heroku cli在Heroku部署的应用上运行命令时:

heroku run npm run commands

我得到打字错误:

ts-node --files deploy-commands.ts
/app/node_modules/ts-node/src/index.ts:820
return new TSError(diagnosticText, diagnosticCodes);
^
TSError: ⨯ Unable to compile TypeScript:
src/commands/picks.ts:3:26 - error TS7016: Could not find a declaration file for module 'luxon'. '/app/node_modules/luxon/build/node/luxon.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/luxon` if it exists or add a new declaration (.d.ts) file containing `declare module 'luxon';`
3 import { Settings } from 'luxon'
~~~~~~~
at createTSError (/app/node_modules/ts-node/src/index.ts:820:12)

我的包。json包含devDependencies中的类型(我知道Heroku会把这些去掉)

"devDependencies": {
"@types/luxon": "^2.3.1",
},

所以我添加了types/index.d。ts:

declare module 'luxon'

但是仍然得到错误。

直接的问题是@types/luxon被声明为开发依赖项,但是Heroku在默认情况下构建应用程序段后剥离devDependencies。它们在运行时不可用

你可以禁用开发依赖项修剪或将该依赖项移动到你的dependencies,但更好的选择是在运行时不运行TypeScript。

build脚本中将其编译为JavaScript,然后运行JavaScript,例如:
"scripts": {
"build": "tsc",
"commands": "node deploy-commands.js",
},

最新更新