stc icon indicating copy to clipboard operation
stc copied to clipboard

`extends` should handle `string` and a template literal type

Open kdy1 opened this issue 1 year ago • 5 comments

Problem

This is a follow-up of https://github.com/dudykr/stc/pull/212


warning: Type
  --> $DIR/tests/visualize/types/literal/templateLiteralTypes2.ts:96:12
   |
96 | const t4 = takesLiteral(id4);  // unknown
   |            ^^^^^^^^^^^^^^^^^
   |
   = note: string extends `foo.bar.${infer R}` ? {
           } : unknown

It should be unknown instead of being a conditional type

warning: Type --> $DIR/tests/visualize/types/literal/templateLiteralTypes2.ts:90:12 | 90 | const t2 = takesLiteral(id2); // "baz" | ^^^^^^^^^^^^^^^^^ | = note: "foo.bar.baz" extends foo.bar.${infer R} ? { } : unknown


It should be "baz" (R) instead.

Solution

We should add a special case for child = string and parent = template literal type to https://github.com/dudykr/stc/blob/269f6c07676f728e667ca5fb4c80d7f17f66a98d/crates/stc_ts_file_analyzer/src/analyzer/generic/expander.rs#L125-L136

kdy1 avatar Nov 05 '22 19:11 kdy1

I'm working on this.

AcrylicShrimp avatar Nov 06 '22 13:11 AcrylicShrimp