You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interfaceUser{editorParams?: SelectParams|AutoCompleteParams;}letuser: User;letautoComplete1: AutoCompleteParams={searchFunc: (term,values)=>{//search for exact matchesvarmatches: string[]=[];//return matches;},listItemFormatter: function(value,title){//prefix all titles with the work "Mr"return"Mr "+title;},};user.editorParams=autoComplete1;user.editorParams={searchFunc: (term,values)=>{//search for exact matchesvarmatches: string[]=[];//return matches;},listItemFormatter: function(value,title){//prefix all titles with the work "Mr"return"Mr "+title;},};interfaceSelectParams{listItemFormatter?: (value: string,text: string)=>string;}interfaceAutoCompleteParams{listItemFormatter?: (value: string,text: string)=>string;searchFunc: (term: string,values: string[])=>string[];}
Expected behavior:
I expected when commenting out the return statement in the inferred usage of searchFunc that a compiler error would occur. The annotated method above does give the compiler error. The playground link shows this as well.
Actual behavior:
No compiler error for the inferred usage. Hovering the inferred searchFunc in VSCode shows it as AutoCompleteParams as well.
From the checker's perspective, you've created a valid SelectParams object which happens to have an extraneous searchFunc, which is in turn not flagged as an excess property because it's a valid property name in AutoCompleteParams. The combination of the last two things is unfortunate but each of those behaviors is a local maximum.
In general you should try hard to not have a union type T | U where U is a subtype of T because it tends to produce odd behavior like that. @j-oliveras 's suggestion is a good one.
TypeScript Version: 3.3.3
type inference
optional properties
Code
Expected behavior:
I expected when commenting out the return statement in the inferred usage of searchFunc that a compiler error would occur. The annotated method above does give the compiler error. The playground link shows this as well.
Actual behavior:
No compiler error for the inferred usage. Hovering the inferred searchFunc in VSCode shows it as AutoCompleteParams as well.
Playground Link:
https://www.typescriptlang.org/play/#src=interface%20User%7B%0D%0A%20%20%20%20editorParams%3F%3A%20SelectParams%20%7C%20AutoCompleteParams%3B%0D%0A%7D%0D%0A%0D%0Alet%20user%3A%20User%3B%0D%0A%0D%0Alet%20autoComplete1%3A%20AutoCompleteParams%20%3D%20%7B%0D%0A%20%20%20%20searchFunc%3A%20(term%2C%20values)%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%2F%2Fsearch%20for%20exact%20matches%0D%0A%20%20%20%20%20%20var%20matches%3A%20string%5B%5D%20%3D%20%5B%5D%3B%0D%0A%20%20%20%20%20%20%2F%2Freturn%20matches%3B%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20listItemFormatter%3A%20function(value%2C%20title)%20%7B%0D%0A%20%20%20%20%20%20%2F%2Fprefix%20all%20titles%20with%20the%20work%20%22Mr%22%0D%0A%20%20%20%20%20%20return%20%22Mr%20%22%20%2B%20title%3B%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%7D%3B%0D%0A%20%20user.editorParams%20%3D%20autoComplete1%3B%0D%0A%20%20%0D%0A%20%20user.editorParams%20%3D%20%7B%0D%0A%20%20%20%20searchFunc%3A%20(term%2Cvalues)%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%2F%2Fsearch%20for%20exact%20matches%0D%0A%20%20%20%20%20%20var%20matches%3A%20string%5B%5D%20%3D%20%5B%5D%3B%0D%0A%20%20%20%20%20%20%2F%2Freturn%20matches%3B%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20listItemFormatter%3A%20function(value%2C%20title)%20%7B%0D%0A%20%20%20%20%20%20%2F%2Fprefix%20all%20titles%20with%20the%20work%20%22Mr%22%0D%0A%20%20%20%20%20%20return%20%22Mr%20%22%20%2B%20title%3B%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%7D%3B%0D%0A%0D%0A%20%20interface%20SelectParams%20%7B%0D%0A%20%20%20%20listItemFormatter%3F%3A%20(value%3A%20string%2C%20text%3A%20string)%20%3D%3E%20string%3B%0D%0A%20%20%7D%0D%0A%0D%0A%20%20interface%20AutoCompleteParams%20%7B%0D%0A%20%20%20%20listItemFormatter%3F%3A%20(value%3A%20string%2C%20text%3A%20string)%20%3D%3E%20string%3B%0D%0A%20%20%20%20searchFunc%3A%20(term%3A%20string%2C%20values%3A%20string%5B%5D)%20%3D%3E%20string%5B%5D%3B%0D%0A%20%20%7D
Related Issues:
Possibly
#27704
The text was updated successfully, but these errors were encountered: