Skip to content

Type inference not producing error #30175

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

Closed
Jojoshua opened this issue Mar 1, 2019 · 3 comments
Closed

Type inference not producing error #30175

Jojoshua opened this issue Mar 1, 2019 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@Jojoshua
Copy link

Jojoshua commented Mar 1, 2019

TypeScript Version: 3.3.3

type inference
optional properties

Code

interface User{
    editorParams?: SelectParams | AutoCompleteParams;
}

let user: User;

let autoComplete1: AutoCompleteParams = {
    searchFunc: (term, values) => {
      //search for exact matches
      var matches: 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 matches
      var matches: string[] = [];
      //return matches;
    },
    listItemFormatter: function(value, title) {
      //prefix all titles with the work "Mr"
      return "Mr " + title;
    },
  };

  interface SelectParams {
    listItemFormatter?: (value: string, text: string) => string;
  }

  interface AutoCompleteParams {
    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.

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

@Jojoshua Jojoshua changed the title Type inference Type inference not producing error Mar 1, 2019
@j-oliveras
Copy link
Contributor

Workaround:

  interface SelectParams {
    listItemFormatter?: (value: string, text: string) => string;
    searchFunc?: undefined; // Added this
  }

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Mar 5, 2019
@RyanCavanaugh
Copy link
Member

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.

@Jojoshua
Copy link
Author

Jojoshua commented Mar 6, 2019

@RyanCavanaugh How did the mouse hover in VSCode get it right? It knew which interface to pull details for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants