language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

Incorrect type narrowing in closures

Open MellKam opened this issue 10 months ago • 5 comments

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) image

A bit different, but still incorrect reproduction in playground

image

MellKam avatar Apr 23 '24 15:04 MellKam

I can't reproduce this issue on my machine, could you please provide a full repro repository?

so1ve avatar Apr 24 '24 01:04 so1ve

@so1ve You can check the vue playground link. I will try to make a repo with reproduction today

MellKam avatar Apr 24 '24 07:04 MellKam

I already checked that, and indeed the type is incorrect. But I cannot debug on the playground :(

so1ve avatar Apr 24 '24 08:04 so1ve

@so1ve https://github.com/MellKam/vue-lsp-ts-bug/blob/main/src/App.vue

MellKam avatar Apr 25 '24 16:04 MellKam

The type is still incorrect after I disabled the Vue extension 🤔

so1ve avatar May 09 '24 05:05 so1ve

@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: image

vue playground

MellKam avatar Jun 18 '24 18:06 MellKam

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.

TypeScript playground with strictNullChecks disabled

kermanx avatar Jun 19 '24 03:06 kermanx

@KermanX Thanks for explaination ❤️

MellKam avatar Jun 19 '24 15:06 MellKam