gpt4 book ai didi

javascript - 期望 typescript tsc 抛出错误,但没有抛出任何错误

转载 作者:行者123 更新时间:2023-12-04 10:24:34 24 4
gpt4 key购买 nike

在用于编辑 typescript 代码的 IDE 中,checkApp.ts 中有一条警告,说明:

Argument type { someWrongParams: any } is not assignable to parameter type AddAppToListParams.

但是,当我运行 tsc没有抛出错误。如何在运行时显示错误 tsc .

tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"noEmit": true,
"pretty": true,
"strict": true,
"strictFunctionTypes": true,
"noImplicitReturns": true,
"module": "ES6",
"target": "ES6"
},
"exclude": [
"node_modules",
"dist"
],
"include": [
"./src/**/*"
]}

代码
checkApp.ts
interface CheckAppParams {
appId: string;
}

export default (app: any) => async (requestParams: CheckAppParams): Promise<any> => {
const { appId } = requestParams;
await app.tasks.addAppToList({ someWrongParams: appId });
};

addAppToList.ts
interface AddAppToListParams {
appId: string;
}

export default (app: any) => async (requestParams: AddAppToListParams): Promise<any> => {
const { appId } = requestParams;
app.list.push(appId);
};

任务.ts
import checkApp from './checkApp';
import addAppToList from './addAppToList';

export default (app: any) => async (): Promise<any> => {
app.tasks = {
addAppToList: addAppToList(app),
checkApp: checkApp(app),
};
};

最佳答案

如果你想让 tsc 抛出一个错误,你可以定义 app作为 interface并引用它而不是 any ,否则 typescript 编译器将不知道它是什么。

将您的应用程序定义为接口(interface)将允许编译器识别错误:

export interface App {
tasks: {
addAppToList: (requestParams: AddAppToListParams) => Promise<any>;
checkApp: (requestParams: CheckAppParams) => Promise<any>;
};
list: any; // you can make it whatever it needs to be.
}

这种 interface 的可行性取决于您使用 app .
上面的界面只是一个例子,它可以是你需要的任何东西。

或者 您可以将预构建 Hook 添加到您的 package.json在运行 tsc 之前进行 lint 检查检查 IDE 抛出的警告并阻止 tsc 运行。您必须使用与您的 IDE 相同的 linter 规则,但它允许您向构建添加更严格的检查,而不是仅在 tsconfig.json 中定义的默认 typescript 规则集。

所以而不是 tsc使用 npm run build从你以前跑过的地方 tsc
包.json:
  "scripts": {
"prebuild": "do your lint checks here with can throw errors if you want",
"build": "tsc"
},

关于javascript - 期望 typescript tsc 抛出错误,但没有抛出任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60680720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com