-
-
Notifications
You must be signed in to change notification settings - Fork 66
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
expectAssignable and expectNotAssignable not behaving correctly on arrays #199
Comments
Did you try: expectNotAssignable<myCustomType>({ value: [1 as const] }); |
I tried that and that approach works, but it kind of looks a bit meeh to do something like this: expectNotAssignable<myCustomType>({ value: [1 as const, 1 as const, 1 as const] }); For a slightly longer type, I feel there are way too many type myCustomType = {
simple: 1;
optional: 2 | null;
list: 3[];
}
expectAssignable<myCustomType>({
simple: 1,
optional: 2,
list: [3, 3, 3]
}); // ✅ it passes, but should this have 'as const' as well?
expectNotAssignable<myCustomType>({
simple: 1 as const,
optional: 2 as const,
list: [3 as const, 3 as const, 3 as const]
}); // ✅ only way to make it fails as expected
expectNotAssignable<myCustomType>({
simple: 1, // not made 'as const'
optional: 2 as const,
list: [3 as const, 3 as const, 3 as const]
}); // ❌ it passes when it should fail
expectNotAssignable<myCustomType>({
simple: 1 as const,
optional: 2, // not made 'as const'
list: [3 as const, 3 as const, 3 as const]
}); // ❌ it passes when it should fail
expectNotAssignable<myCustomType>({
simple: 1, // not made 'as const'
optional: 2, // not made 'as const'
list: [3 as const, 3 as const, 3 as const]
}); // ❌ it passes when it should fail
expectNotAssignable<myCustomType>({
// make the full object 'as const'
simple: 1,
optional: 2,
list: [3, 3, 3]
} as const); // ❌ it passes when it should fail Thoughts? |
Thanks for asking. It is always interesting to talk about type testing. Please note that I am not associated with It is going to be replaced by brand new type testing tool, which I will publish in about a week. It is written from scratch, the architecture is different and all that helps to solve limitations which Back to your problem. What do you mean by “slightly longer type”? In this case To check how type information is inferred, you can use hover information: But: That is just how TypeScript works. Use All this is simple and obvious, of course. (Might be even intuitive, but that’s rather subjective.) The problem is that But In a way, Hope this explains the inconsistency of behaviour between |
Gotcha, thanks for the explanation 😄 By “slightly longer type”, I meant this: type myCustomType = {
simple: 1;
optional: 2 | null;
list: 3[];
} In the initial version where // I'd prefer to do something like this (which doesn't work)
expectNotAssignable<myCustomType>({
simple: 1,
optional: 2,
list: [3, 3, 3]
} as const);
// Instead of marking everything as const
expectNotAssignable<myCustomType>({
simple: 1 as const,
optional: 2 as const,
list: [3 as const, 3 as const, 3 as const]
}); I kind of expected Looking forward to |
Hey
I noticed that the behaviour for
expectAssignable
andexpectNotAssignable
seems wrong for arraysHere are a few examples;
✅ Works as expected
❌ Fails
I saw that in #190 a suggested fix would be to use
as const
but this doesn't really work with arraysAny suggestions?
The text was updated successfully, but these errors were encountered: