Skip to content
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

K extends keyof ... not being narrowed in a switch case #31743

Closed
YePpHa opened this issue Jun 3, 2019 · 2 comments
Closed

K extends keyof ... not being narrowed in a switch case #31743

YePpHa opened this issue Jun 3, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@YePpHa
Copy link

YePpHa commented Jun 3, 2019

TypeScript Version: 3.5.1

Search Terms:
extends keyof narrowing
infer not narrowing type

Code

type MethodArguments<T> = [T] extends [(...args: infer U) => any]
  ? U
  : [T] extends [void] ? [] : [T];

interface User {
  setName(name: string): void;
  setAge(age: number): void;
  setBirthday(year: number, month: number, day: number): void;
  getName(): string;
  getAge(): number;
  getBirthday(): { year: number, month: number, day: number };
}

class SomeClass {
  constructor(public user: User) { }

  handle<K extends keyof User>(method: K, ...args: MethodArguments<User[K]>): void {
    let result: ReturnType<User[K]>;

    switch (method) {
      case "setName":
        const [
          name
        ] = args;

        result = this.user.setName(name);

        break;
      case "setAge":
        const [
          age
        ] = args;

        result = this.user.setAge(age);

        break;
      case "setBirthday":
        const [
          year,
          month,
          day
        ] = args;

        result = this.user.setBirthday(year, month, day);

        break;
      case "getName":
        result = this.user.getName();

        break;
      case "getAge":
        result = this.user.getAge();

        break;
      case "getBirthday":
        result = this.user.getBirthday();

        break;
    }

    console.log(result);
  }
}

Expected behavior:
In the switch case each case should narrow the type of method, args and result. However, this doesn't happen.

Actual behavior:
What's actually occurring is that in each switch case, method has the type K extends "setName" | "setAge" | "setBirthday" | "getName" | "getAge" | "getBirthday". Its type is not being narrowed to one of the keys of User. This problem also persist for the args and result variables. They're not being narrowed to their respective types.

@jcalz
Copy link
Contributor

jcalz commented Jun 3, 2019

Duplicate of #13995 / #20375 ?

... also related to #27808 / #25879?

@YePpHa
Copy link
Author

YePpHa commented Jun 3, 2019

@jcalz yeah, seems like it's the same issue as #13995.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 4, 2019
@YePpHa YePpHa closed this as completed Jun 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants