Skip to content

typeguard else statement is not working properly #42185

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
ATheCoder opened this issue Jan 3, 2021 · 2 comments
Closed

typeguard else statement is not working properly #42185

ATheCoder opened this issue Jan 3, 2021 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@ATheCoder
Copy link

Bug Report

πŸ”Ž Search Terms

Typeguard else not working

Type guard not working

πŸ•— Version & Regression Information

  • I first had this issue on version 3.9.5 but I also tested it on the playground nightly and it still happens.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const regularNameToUnicodeMap = {
  Home: String.fromCharCode(0xf262),
  Plugin: String.fromCharCode(0xf261),
  Tree: String.fromCharCode(0xf25f),
  Schema: String.fromCharCode(0xf25e),
};

const solidNameToUnicodeMap = {
  Search: String.fromCharCode(0xf002),
};

interface IconProps<T extends 'regular' | 'solid'> {
  type: T;
  icon: T extends 'regular'
    ? keyof typeof regularNameToUnicodeMap
    : keyof typeof solidNameToUnicodeMap;
}

type RegularIcon = keyof typeof regularNameToUnicodeMap;
type SolidIcon = keyof typeof solidNameToUnicodeMap;

const isRegular = (
  type: 'regular' | 'solid',
  icon: RegularIcon | SolidIcon,
): icon is RegularIcon => {
  return type === 'regular';
};

const P = <T extends 'solid' | 'regular'>(props: IconProps<T>): any => {
        if(isRegular(props.type, props.icon)){
                return regularNameToUnicodeMap[props.icon]
        } else {
                return solidNameToUnicodeMap[props.icon] //Error happens here.
        }
}

πŸ™ Actual behavior

I get an error:

Type 'T extends "regular" ? "Home" | "Plugin" | "Tree" | "Schema" : "Search"' cannot be used to index type '{ Search: string; }'

πŸ™‚ Expected behavior

There shouldn't be an error because according to the docs typescript should know that when the type guard returns false icon is of type SolidIcon.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jan 4, 2021
@RyanCavanaugh
Copy link
Member

Given how narrowing and generics interact today, we don't have any way to represent the type of props.icon in the else branch without negated types; see #29317

@sladiri
Copy link

sladiri commented Mar 2, 2021

Hi, is this the same problem?
I have a very minimal example: https://stackblitz.com/edit/typescript-s2c1jm?file=index.ts

And this looks like a variation? I hoped, I found a workaround: https://stackblitz.com/edit/typescript-itubna?file=index.ts

I guess you can always use switch cases:
https://stackblitz.com/edit/typescript-ut4l46?file=index.ts

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