我刚刚安装了 Deno 1.9.2 并在我的 PC 上打开了一个空白文件夹。我正在学习有关 TypeScript 基础知识的教程。这就是我的问题所在。
const someFunc = (n: number) => {
if (n % 2 === 0) {
return "even"
}
}
const value = someFunc(4)
value.substring(1)
有一次 VSCode 给老师一个关于值的内联警告,说 Object is possibly 'undefined'.
我环顾四周,并被告知将我的 VSCode typescript.validate.enable 更改为 true。我已经这样做了,并且我已经多次重新启动 VSCode。当我使用 deno run index.ts
运行我的代码时,出现错误。有什么想法吗?
答案 0 :(得分:1)
此错误与 VSCode 或 Deno 无关。
在您提供的代码中,函数 someFunc
的返回类型为 string | undefined
。我的猜测是您的 tsconfig.json
有 strictFunctionTypes: true
。
您可以通过以下方式修复此错误:
正确处理所有情况-
const someFunc = (n: number) => {
if (n % 2 === 0) {
return 'even';
}
return ''
};
或在您的 strictFunctionTypes
中禁用 tsconfig.json
-
{
"compilerOptions": {
"strictFunctionTypes": false,
"plugins": [
{
"name": "typescript-deno-plugin",
"enable": true, // default is `true`
"importmap": "import_map.json"
}
]
}
}
虽然我不确定 Deno 是否可以在没有 strictFunctionTypes
的情况下工作。