language-tools
language-tools copied to clipboard
Incorrect type narrowing in closures
The type narrowing seems to be incorrect. In this example it should be the same type as the variable originally has, but for some reason it has null
. (I have the latest version lsp extension and the latest version of vscode installed)
A bit different, but still incorrect reproduction in playground
I can't reproduce this issue on my machine, could you please provide a full repro repository?
@so1ve You can check the vue playground link. I will try to make a repo with reproduction today
I already checked that, and indeed the type is incorrect. But I cannot debug on the playground :(
@so1ve https://github.com/MellKam/vue-lsp-ts-bug/blob/main/src/App.vue
The type is still incorrect after I disabled the Vue extension 🤔
@so1ve Yeah, it seems that this result is correct from the point of view of typescript. It was a big discovery for me. I even found how to fool this "inference".
let x: string | null = null;
const setX = () => x = "foo";
setX();
typeof x;
// ^? let x: null
With the latest version of vscode extension, types seem to be the same as in .ts files, but the vue playground still shows wrong results. Like this one:
With the latest version of vscode extension, types seem to be the same as in .ts files, but the vue playground still shows wrong results. Like this one:
This is because compilerOptions.strictNullChecks
is off in Vue playground. Not a Vue issue.
@KermanX Thanks for explaination ❤️