为什么从字符串数组中推断第一个元组值会丢失其类型?

huangapple go评论60阅读模式
英文:

Why does inferring the first tuple value from string[] lose it's type?

问题

When inferring the first tuple value from a string array, it seems like TypeScript loses the information that the inferred value must be a string. Why is the type lost?

type Test<T extends string> = true;
// Head should be known to be a string since T is string[]
type Example<T extends string[]> = T extends [infer Head, ...infer Tail]
  ? // Type 'Head' does not satisfy the constraint 'string'.
    Test<Head>
  : never;

I know I can solve the issue by using infer Head extends string instead of infer Head, but I don't understand why it's necessary. Is there some scenario I'm missing where Head could be something other than a string?

英文:

When inferring first tuple value from string array, it seems like TypeScript loses the information that the inferred value must be string. Why is the type lost?

type Test&lt;T extends string&gt; = true;
// Head should be known to be string since T is string[]
type Example&lt;T extends string[]&gt; = T extends [infer Head, ...infer Tail]
  ? // Type &#39;Head&#39; does not satisfy the constraint &#39;string&#39;.
    Test&lt;Head&gt;
  : never;

I know I can solve the issue by doing infer Head extends string instead of infer Head, but I don't understand why it's necessary. Is there some scenario I'm missing where Head could be something else than string?

Link to TS playground

答案1

得分: 1

You're not really missing anything; the type of Head will always be assignable to string in your example, but the compiler just doesn't see it.

There is an open feature request at microsoft/TypeScript#51108 to use information from generic constraints to further narrow the type of type parameters inferred in conditional types. If you want to see this implemented, it wouldn't hurt to give that issue a 👍 and possibly describe your use case if it's novel and compelling. It probably wouldn't help much, but it wouldn't hurt.

Until and unless the feature is implemented, you'll have to work around it. The easiest workaround is probably to use a redundant extends constraint on inferred type parameters, as you mentioned.

英文:

You're not really missing anything; the type of Head will always be assignable to string in your example, but the compiler just doesn't see it.

There is an open feature request at microsoft/TypeScript#51108 to use information from generic constraints to further narrow the type of type parameters inferred in conditional types. If you want to see this implemented, it wouldn't hurt to give that issue a 👍 and possibly describe your use case if it's novel and compelling. It probably wouldn't help much, but it wouldn't hurt.

Until and unless the feature is implemented, you'll have to work around it. The easiest workaround is probably to use a redundant extends constraint on inferred type parameters, as you mentioned.

huangapple
  • 本文由 发表于 2023年5月6日 22:11:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定