Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numeric index type should not merge #61088

Open
nnnnoel opened this issue Jan 31, 2025 · 1 comment
Open

numeric index type should not merge #61088

nnnnoel opened this issue Jan 31, 2025 · 1 comment

Comments

@nnnnoel
Copy link

nnnnoel commented Jan 31, 2025

πŸ”Ž Search Terms

tuple to discriminated union

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about `Common "Bugs" That Aren't Bugs

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20250130#code/FAFwngDgpgBAcgVwLZQE4EsDGBlEGB2A5jALwwAGAJAN77IBGaAvuQNyiSwAqA9oko1QAeLjCgAPEFHwATAM4w5edEQB8pGKIlTZCqtRUAzNDACqYydPkw6A5uRgB+MzABcNqADc07YBIg8qCAw4NAwuFAQALI8MlAANgCMIhY61qhQAIYyPPjxYDAA2giyUIYqUDIANDAAdPUlANb4PADu+IUAup3qZNTAMEUA0jAqMACikqiZmCBCjVBgPIaaNbaCqp3u-YODSpHuQ+y7MEixCe5chUOdxzBM7EyFk3gzcwtLK1xrDGibvv5AsFQrAItFzvEAEwpbRWBQZbK5fJFEpxcr4So1eq1JotdpdHoaHbDUb4CZTN7zRbLVbwZBoLC4AiETbbAa7fYQS58X7CIaqO6DM5xeKXQq8fiCIT827sh7AJ4vaazKmfWn8Bk4ZRqWUcMIxEWJDRgg0JZLFUrozEwagwTyZeIIKDudYmJg9dgAek9uwAeo5gHrYKaocapOCRdCLWiKtUbXaHU6Xbz7h7gN6-Y4gA

πŸ’» Code

type NumericString = `${number}`;
type ToNumber<T extends string> = T extends `${infer U extends number}` ? U : never;

export type StepModel1<T extends readonly [undefined, ...unknown[]]> = {
  [K in Extract<keyof T, number>]: {
    step: K;
    model: T[K];
  };
}[Extract<keyof T, number>];

export type StepModel2<T extends readonly [undefined, ...unknown[]]> = {
  [K in Extract<keyof T, NumericString>]: {
    step: ToNumber<K>;
    model: T[ToNumber<K>];
  };
}[Extract<keyof T, NumericString>];

type Model1 = StepModel1<[undefined, { value: number }]>;
//   ^?

type Model2 = StepModel2<[undefined, { value: number }]>;
//   ^?

πŸ™ Actual behavior

Model1 union types are merged as number type

πŸ™‚ Expected behavior

Model1 type also be same with Model2

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Jan 31, 2025

Basically a duplicate of #55762

This is working as intended. Yes, it's weird that the known numeric indices of tuples are string literal types and not numeric literal types, but it's a well-established weirdness that's been around for a long time. It's not a bug, anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants