TypeScript Type Error when hover in a variable

I am writing a type to get the last element type of a tuple, see on type-challenges: https://github.com/type-challenges/type-challenges/blob/main/questions/00015-medium-last/README.md

And the IDE get the wrong type. When I hover to the variable,  it shows never type. It's completely wrong and it makes me very confuse.

type arr = ['a', 'c']
type Last<T> = T extends [...any[], infer L] ? L : never
type tail = Last<arr1> // it should be 'c', but it give never
// But when I assign the type to a variable, it works
const a: tail = 'c' // Hover to a, the type is 'c', correct.

I try it on typescript Playground, and vscode, it all work. Only Webstorm got the wrong answer. Similar questions appear many times. And I close webstorm and restart it, it doesn't work as the same.

Is there a issue for that?

评论操作 固定链接

The type is correctly inferred for me - here are the results shown on hover and Ctrl+hover:

 

what IDE version do you use?

1
评论操作 固定链接

Sorry I don't find a button to reply your comment so I write a comment down here, hope you can see it.

I use WebStorm 2022.2.2, Apple Silicon Version.

The result down here.

you can see that, the type is never. But I assign a variable to this type, it works. It should be '3' type.

0
评论操作 固定链接

Well, when I press the command key and hover to the type, it works. The picture is down here.

It works but only when I press the command key and hover to the type.

But I still feel sad about it..I don't want to press another key when I need to check the type..

0
评论操作 固定链接

Could you check if you can reproduce the issue in a new project using the code snippet from your previous post? What does your tsconfig.json look like?

0
评论操作 固定链接

I check again the code.

It's OK when the `arr` type is only two members. More than two is not OK!!

The code I write is type arr = [1, 2], only two elements in the tuple type. When it comes to three or more, It won't work.

type arr = [1, 2]
// rest of the code
type resultOfTwoElement = Last<arr> // expect to be 2, ok!
type arr3 = [1, 2, 3]
type resultOfThreeElement = Last<arr3> // Not 3!

You could check it on your WebStorm, it's not about the tsconfig.json, it's just wrong..

Unless I press the command key and hover to the variable, It works.

0

请先登录再写评论。