I have just installed Deno 1.9.2 and opened up a blank folder on my PC. I am following a tutorial on the basics of TypeScript. This is where my problem is.
const someFunc = (n: number) => {
if (n % 2 === 0) {
return "even"
}
}
const value = someFunc(4)
value.substring(1)
At one point VSCode gives the teacher an inline warning on value saying Object is possibly 'undefined'.
I've looked around and was told to change my VSCode typescript.validate.enable to true. I've done this and I've restarted VSCode multiple times. When I run my code with deno run index.ts
, I get my error. Any ideas?
This error is not related to VSCode or Deno.
In the code you provided function someFunc
has return type of string | undefined
. My guess is that your tsconfig.json
has strictFunctionTypes: true
.
You can fix this error by:
Correctly handling all cases -
const someFunc = (n: number) => {
if (n % 2 === 0) {
return 'even';
}
return ''
};
Or disabling strictFunctionTypes
in your tsconfig.json
-
{
"compilerOptions": {
"strictFunctionTypes": false,
"plugins": [
{
"name": "typescript-deno-plugin",
"enable": true, // default is `true`
"importmap": "import_map.json"
}
]
}
}
Although I am not sure if Deno can work without strictFunctionTypes
.