1

I'm a new comer in typescript, and I learnt that we can not assign Type 'string | undefined' to Type 'string' from internet tutorials like https://linguinecode.com/post/how-to-solve-typescript-possibly-undefined-value. But when I write a demo on WebStorm, I found it can be compiled, without any errors, and also can run. This totally confused me.

Here is my TS code:

function validateToken(token: string) {
    return token;
}

function run() {
    const token = 'kjadj' as string | undefined;

    let a = validateToken(token);
    console.log(a)
}

after compile , it became below JS code:

function validateToken(token) {
    return token;
}
function run() {
    var token = 'kjadj';
    var a = validateToken(token);
    console.log(a);
}

My typescript version is 4.7.4, WebStorm version is Build #WS-221.5921.27, built on June 22, 2022.

Thanks for you guys' help. Followed suggestions in comment area, so far my tsconfig.json is:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true,
    "strictNullChecks": true,
    "strict": true,
  },
  "exclude": [
    "node_modules"
  ]
}

strictNullChecks option does work in TS playground, but it doesn't work in my local WebStorm either, also, I can compile my ts file via tsc demo.ts command. BTW this's my file constructor:

.
├── demo.js.map
├── index.html
├── demo.ts
├── demo.js
├── tsconfig.json
1

1 Answer 1

2

You need to activate strictNullChecks

To test out how differnt typescript configs behave you can use your sample in typescriptPlayground. Under the "TS Config" dropdown you can easily swap settings ;)

Update: As you updated the question and added a commend: The reason for tsc somefile to still work fine is likely:

When input files are specified on the command line, tsconfig.json files are ignored.

Explained in the typescript handbook

So the easiest solution for you is probably to set the files in the tsconfig file, or even better the include and just use tsc as command ;)

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

5 Comments

Thanks, dude. I tried it, but found although the web page will mark line 8 with red underline, but it still can work. When i click 'run' button, it will output 'kjadj' into Logs area.
Dear Mattstir without strictNullChecks option it also shows the error but when you try to compile the .ts file it simply compiles into .js file without showing any error in terminal
Thank you Mattstir, I think you 99% resolve my confusion. When I enable strictNullChecks in my tsconfig.json, and follow your suggestion, use tsc instead of tsc demo.ts, the compiler throw an error. But finally it compile a js file, and it's runnable.
Why are there such strange results? Is the compiler's error just a guideline?
As normal javascript is also valid Typescript depending on your settings it really comes down to what you configure (how strict, etc..) ;) If my answer answered your original question it would be nice if you accept it ;) thx

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.