twoslash
twoslash copied to clipboard
twoslash renderer not highlighting errors that span multiple tokens
I'm trying to render an error for this block of code:
type MyPick<T, K> = {
[Key in K]: T[Key]
}
I'm using this twoslash
block:
```ts twoslash
// @errors: 2322 2536
type MyPick<T, K> = {
[Key in K]: T[Key]
}
```
That block results in this output:
The error output is correct. There are two errors: one on K
and another on T[Key]
. However, as you can see there's no error squiggly for T[Key]
.
Here's what it should look like (screenshot from ts playground):
I believe this has two causes:
-
@typescript/twoslash
parsesT[Key]
as 4 tokens:[ { content: 'T', color: '#267F99', fontStyle: 0, explanation: [Array], }, { content: '[', color: '#000000', fontStyle: 0, explanation: [Array], }, { content: 'Key', color: '#267F99', fontStyle: 0, explanation: [Array], }, { content: ']', color: '#000000', fontStyle: 0, explanation: [Array], }, ]
-
The logic for
findTokenFunc
inrenderers/twoslash.ts
expects a single token: https://github.com/shikijs/twoslash/blob/fbdf0a217bb314ee6406dd231a31adedf95fb1ea/packages/shiki-twoslash/src/renderers/twoslash.ts#L93-L94
Here's the error itself as reported by @typescript/twoslash
:
{
category: 1,
code: 2536,
length: 6,
start: 36,
line: 1,
character: 14,
renderedMessage: "Type 'Key' cannot be used to index type 'T'.",
id: 'err-2536-36-6'
}
Using findTokenDebug
in place of findTokenFunc
gives the following results (truncated to relevant output):
false 14 <= 14 && 15 >= 20
false 15 <= 14 && 16 >= 20
false 16 <= 14 && 19 >= 20
false 19 <= 14 && 20 >= 20
So this fails in two ways:
-
start
is only<=
toe.character
for the first character of the tokens. -
start + token.content.length
is only>=
e.character + e.length
for the last character of the tokens.
It seems like what we'd need to do is apply the error to any token that falls within the range of the error. I'm not super familiar with the codebase though, so I'm not sure what the implications of making that change would be (if any).
Happy to tackle this myself if anyone could give me a little direction here.