Skip to content

Function property in union does not get parameter type inferred from result of type narrowingΒ #63088

@dyantako

Description

@dyantako

πŸ”Ž Search Terms

type narrowing inferred function parameter union discriminant

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about unions

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.3&ssl=17&ssc=52&pln=1&pc=1#code/JYOwLgpgTgZghgYwgAgGIHt3IN4ChnIBucANgK4QBcyAzmFKAOYDc+tEYAaqRdQBTFyVWvSYBKZAF4AfEXTAAJqwC+uXKEixEKAEJwoONoN7IQZALYAjaKwI0O3If2PCzV6BJlzFKtWACeAA4oAPKBYMDoIDRSaJjIAD7IelCsuDBkIAgRUcgwIHzo4ZHR1GE50RJ4BFAcZFAgpmQkJL7pBdhEPMIARD0ANOxc3fwQnrIIUTToJBAAdCTojHxjyMpizEA

πŸ’» Code

interface Foo {
  value: string;
  setValue: (value: string) => void;
}

interface Bar {
  value: number;
  setValue: (value: number) => void;
}

type Options = Foo | Bar;

function fn(options: Options) {
  return null;
}

fn({ value: "", setValue: (e) => console.log(e) });

πŸ™ Actual behavior

The e parameter of setValue is inferred as any rather than string, despite value being a string which means Foo is the only valid type.

πŸ™‚ Expected behavior

setValue here should have e inferred as a string

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions