-
Notifications
You must be signed in to change notification settings - Fork 8
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
v0.16.0 no longer distinguishes between "{number}" and number object keys #34
Comments
Passing is the right behaviour afaics. Consider how this is valid in both TypeScript and Runtime: type T = {
1: string;
"2": string;
3: string;
"4": string;
};
const t: T = {
1: 'foo',
2: 'foo',
"3": 'foo',
"4": 'foo',
} That means type R = { 1: string }
type S = { '1': string }
function foo(r: R, s: S) {
r = s
s = r
} Meaning assigning objects with There is no meaningful difference between None of these two indicate a case for your test example to show an error. Or am I missing the point? Don't want to hijack your issue, but what I find however interesting is why the third line passes here: expectTypeOf<{ 1: string }>().toMatchTypeOf<Record<number, string>>()
expectTypeOf<{ '1': string }>().toMatchTypeOf<Record<number, string>>()
expectTypeOf<{ 'a': string }>().toMatchTypeOf<Record<number, string>>() While this related usecase has an error: const bar: Record<number, string> = { 'a': 'foo' } // TS2322: Type `{ a: string; }` is not assignable to type `Record<number, string>` It doesn't make much sense. However this is probably a different issue, as these all seem to pass too: expectTypeOf<Record<string, string>>().toMatchTypeOf<Record<number, string>>() // should be error
expectTypeOf<Record<number, string>>().toMatchTypeOf<Record<string, string>>() // ok, since all number keys are cast to string anyway
expectTypeOf<Record<symbol, string>>().toMatchTypeOf<Record<string, string>>() // should be error
expectTypeOf<Record<symbol, string>>().toMatchTypeOf<Record<number, string>>() // should be error
expectTypeOf<Record<string, string>>().toMatchTypeOf<Record<symbol, string>>() // should be error
expectTypeOf<Record<number, string>>().toMatchTypeOf<Record<symbol, string>>() // should be error
expectTypeOf<Record<symbol, string>>().toMatchTypeOf<Record<number, string>>() // should be error
expectTypeOf<Record<symbol, string>>().toMatchTypeOf<Record<string, string>>() // should be error But |
I can see both arguments. To be honest, distinguishing between number and string keys doesn't feel like the most critical thing, but a theme I'm seeing from recent requests is that there are a few different levels of strictness that people want. I don't think we'll be able to make |
Also note that if you want to test specifically for this, you could do something like this: const x = {[1]: 'one', '2': 'two'}
expectTypeOf<keyof typeof x>().exclude<string>().toEqualTypeOf<1>() But I imagine the reason for this issue is that you don't want to have to know to test for this specifically. |
…r my use cases), add notes about the two issues affected (microsoft/TypeScript#52267 and mmkal/expect-type#34)
First off, excellent work on this package!
Unfortunately, the upgrade to v0.16.0 seems to no longer be able to detect when an object has a
number
property vs. a "stringified""{number}"
property. The following test correctly failed with v0.15.0, but passes with v0.16.0:Typescript itself doesn't really distinguish between these two cases, so I'd understand why this change was made, but I found the nuance helpful. For instance, this is perfectly legal:
The text was updated successfully, but these errors were encountered: