Skip to content

Switch narrowing regression with nested unions #29012

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
jwmerrill opened this issue Dec 13, 2018 · 1 comment
Closed

Switch narrowing regression with nested unions #29012

jwmerrill opened this issue Dec 13, 2018 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis Fixed A PR has been merged for this issue

Comments

@jwmerrill
Copy link

jwmerrill commented Dec 13, 2018

TypeScript Version: 3.2.2, 3.3.0-dev.201xxxxx

Search Terms: 3.2 switch union narrowing regression

Code

type Additive = '+' | '-';
type Multiplicative = '*' | '/';

interface AdditiveObj {
    key: Additive
}

interface MultiplicativeObj {
    key: Multiplicative
}

type Obj = AdditiveObj | MultiplicativeObj

export function foo(obj: Obj) {
    switch (obj.key) {
        case '+': {
            onlyPlus(obj.key);
            return;
        }
    }
}

function onlyPlus(arg: '+') {
  return arg;
}

Expected behavior:
obj.key is narrowed to '+' in the switch and the call to onlyPlus(obj.key) type checks successfully.

Actual behavior:
obj.key is narrowed to Additive in the switch and the call to onlyPlus(obj.key) fails to type check

Playground Link: http://www.typescriptlang.org/play/#src=type%20Additive%20%3D%20'%2B'%20%7C%20'-'%3B%0D%0Atype%20Multiplicative%20%3D%20'*'%20%7C%20'%2F'%3B%0D%0A%0D%0Ainterface%20AdditiveObj%20%7B%0D%0A%20%20%20%20key%3A%20Additive%0D%0A%7D%0D%0A%0D%0Ainterface%20MultiplicativeObj%20%7B%0D%0A%20%20%20%20key%3A%20Multiplicative%0D%0A%7D%0D%0A%0D%0Atype%20Obj%20%3D%20AdditiveObj%20%7C%20MultiplicativeObj%0D%0A%0D%0Aexport%20function%20foo(obj%3A%20Obj)%20%7B%0D%0A%20%20%20%20switch%20(obj.key)%20%7B%0D%0A%20%20%20%20%20%20%20%20case%20'%2B'%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20onlyPlus(obj.key)%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Afunction%20onlyPlus(arg%3A%20'%2B')%20%7B%0D%0A%20%20return%20arg%3B%0D%0A%7D%0D%0A

Related Issues:

Note:
This seems to be a regression between TS 3.1 and TS 3.2

@weswigham weswigham added Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis labels Dec 14, 2018
@weswigham weswigham added this to the TypeScript 3.3 milestone Dec 14, 2018
@noahm
Copy link

noahm commented Dec 19, 2018

Here's another example of a regression in type narrowing behavior from 3.0 that seems related. It seems like the item.user.login is being narrowed correctly in the example but the former is not. If you swap the order of the conditionals in the if statement then the error moves from id to the login property.

export interface FooItem {
  user: {
    id: string;
    login: string;
  };
}

export interface BarItem {
  user: {
    id: string | null;
    login: string | null;
  };
}

function getUser(item: FooItem | BarItem) {
  let user = { id: '', login: '' };
  if (item.user.id && item.user.login) {
    user = {
      id: item.user.id, // Type 'string | null' is not assignable to type 'string'.
      login: item.user.login,
    };
  }
  return user;
}

Playground link (turn on strictNullChecks)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants