0

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?

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. With this little script, I am not using a tsconfig. I'm just trusting deno will handle it. Do you know if there is a tsconfig that deno looks at somewhere where deno is installed?
@NaeNate here is the manual I found. Looks like you can specify tsconfig location by using config option. Welcome to Stack Overflow community.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.