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
Type checker works on union types but it's buggy. #41043
Comments
Duplicate of #20863. Excess property checks are not performed for union types, that's why you don't get an error on the assignment to |
Not sure about what you say. Excess property checks actually work in union types. If you write a wrong key like The other thing, if indexer key types are always strings, in a case that there's only one |
That does surprise me. Excess property checks generelly don't work on union types (at least without a discriminant, see the linked issue). I guess the compiler gets very confused here.
And that error is the excess property check.
In JavaScript properties are always strings. |
|
If you want some property to determine one of the union types, it should be included into all candidates. Here is the fix: playground type Row =
| { iAmString: never; [iAmNumber: number]: number; }
| { iAmString: number; b: number; }
| { iAmString: number; b: number; c: number; }
| { iAmString: number; b: number; c: number; d: number }; |
Well, I don't think so. Now I can't write something like: |
Making
Or I might be missing some reason this is also bad :) |
All of the examples so far represent intended behavior; this is probably better suited for Stack Overflow or other support-type forums. |
It's much better but the thing is that the code below is allowed too (which shouldn't be): |
None of the examples represent the intended behavior. Don't get away from it. |
All of the examples so far represent intended behavior; this is probably better suited for Stack Overflow or other support-type forums. |
We define a type called "Row". we then define a variable called "row" which is an instance of the Row type. The bug is the value of the row variable. The type of the key "iAmNumber" is number, but we wrote the key as "iAmString" which is a string and the type checker validates it as a correct type of value. This is the bug. More detail commented on the code below.
TypeScript Version: 4.0.3
Search Terms:
Code
Expected behavior:
Scenario 1: ERROR: Type '{ iAmString: number; }' is not assignable to type '{ [iAmNumber: number]: number; }'.
Correction: { 58: 45; }
Scenario 2: ERROR: Type '{ iAmString: number; }' is missing the following properties from type '{ iAmString: number; b: number; c: number; d: number; }': b, c, d.
Correction: { iAmString: 45; b: 34; }
Actual behavior:
No error is published. [LOG]: { "iAmString": 45 }
Playground Link:
https://www.typescriptlang.org/play?#code/C4TwDgpgBASg9gdygXgFBQ1APlA3lAbQEsBBAWwDkBXMgIwgCcAuKAOxvoYF0X27GA3FAC+6TDnykyAZWAMirAOa8OgqLRX8GQ0Zmx4oU2fKWbOQjW1XaoAYzNrd4g0bkLlVrRYc37n81AAJj4iAqioADYQwFBwtABWLPBIyHhiGK4mHgAsAKxQAPQFUAAqABbQtFSKhgDOUBUMEAB0pRVQAORx8R0NAIb1APIUAKJQYAxwkAygUH2sgYbAHfWZ7lAIZUS2ZXVzULVuSku1EBEAZq3l0MlQoJD99fOG5NRaUADWECAbRMC7-2g92gfE4zXSekhUIwRTafRiRHqm0YQPaiNqVGgiKu7WBdgqti+DAOZTgVAiiwgf0adzOESgVHqwDgUCarD6ZCx5GM62ZL0o1lixIGHwZTJZfUCi0B40m01mHVoHXBelhljyAHYADSoYRhVC2OCsWpwKLNCJwRQACm6AEoBEA
Related Issues:
The text was updated successfully, but these errors were encountered: