-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
unexpected behavior when use union type #28860
Comments
The Maybe that you expect is a exact type. See discussions about that: #202, #12936 and PR #28749. |
Write it like this instead: export type basetype = { id: string }
export type supertype1 = basetype & { key1?: undefined, key2?: undefined }
export type supertype2 = basetype & { key1: any, key2: any; }
function myfun(p: supertype2 | supertype1) {}
myfun({ id: '' }); // Ok
myfun({ id: '', key1: '' }); // Error
myfun({ id: '', key1: '', key2: '' }); // Ok |
I know structural typing, so I pass an object literal instead of a variable to myfun. And Exact type is what I need. Thank you. |
Nice. It work for me. Thank you. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 3.1.4
Search Terms:
union type
Code
Expected behavior:
{
id: '',
key1: '',
} is not supertype1 or supertype2, when it is used to be a parameter of myfun, typescript should report an error.
Actual behavior:
there is no error.
The text was updated successfully, but these errors were encountered: