-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Required<T> optional properties type validation will not work #29991
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
Comments
This looks to be related to variance markers used for generic mapped types---a very similar problem to #29698. When relating You can see behaviour by replacing type Required2<T> = { [P in keyof T]-?: T[P]; };
const a: Required<{ a?: 1; x: 1 }> = { a: 1, x: 1 };
const b: Required2<{ b?: 1; x: 1 }> = { b: 1, x: 1 };
export let A = a;
export let B = b;
A = b; // Error!
B = a; // Error! I've been wrong on this before though. You'll most likely need @ahejlsberg or @weswigham to give a definitive answer. |
Thanks for looking into this @jack-williams. It was a difficult problem to understand for me. Once it is written as follows, it will be as intended, so it seems to be temporarily resolved. type Required2<T> = Required<{ [P in keyof T]-?: T[P]; }>;
const a: Required2<{ a?: 1; x: 1 }> = { a: 1, x: 1 };
const b: Required2<{ b?: 1; x: 1 }> = { b: 1, x: 1 };
export let A = a;
export let B = b;
A = b; // Property 'a' is missing in type 'Required<{ b: 1; x: 1; }>' but required in type 'Required<{ a: 1; x: 1; }>'.
B = a; // Property 'b' is missing in type 'Required<{ a: 1; x: 1; }>' but required in type 'Required<{ b: 1; x: 1; }>'.
a.b; // Property 'b' does not exist on type 'Required<{ a?: 1; x: 1; }>'.
b.a; // Property 'a' does not exist on type 'Required<{ b?: 1; x: 1; }>'. |
This is fixed in |
@weswigham I can't seem to reproduce: 5bd73f5. It should error on the first two assignments, right? (P.S. excuse my spelling) |
Your baselines are right - getting an error on those two accesses is intended, since the |
Is |
Ah, derp, you're right - I was confusing it with a |
TypeScript Version: 3.2.0-rc <= 3.4.0-dev.20190220
Search Terms: Required
Code
Expected behavior:
When it is "3.1.6" it will be like that.
Actual behavior:
No Error
Playground Link:
link
Related Issues:
The text was updated successfully, but these errors were encountered: