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

Complex generic does not narrow type #44145

Closed
qwelias opened this issue May 18, 2021 · 2 comments
Closed

Complex generic does not narrow type #44145

qwelias opened this issue May 18, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@qwelias
Copy link

qwelias commented May 18, 2021

Bug Report

πŸ”Ž Search Terms

generic narrow type guard object index

πŸ•— Version & Regression Information

v4.2.3

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

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type FO = 'f' | 'o'

type Foo<Type extends FO = FO> = { [T in Type]: {
    type: T,
    data: Record<T, true>,
} }[Type]

const foo = {} as Foo
if (foo.type === 'f') foo.data.f // all good, narrows the type

type Bar<Type extends FO = FO> = { [T in Type]: { foo: Foo<T> } & {
    f: { bar: Record<T, true> },
    o: unknown,
}[T] }[Type]

const bar = {} as Bar
if (bar.foo.type === 'f') bar.bar // broke

πŸ™ Actual behavior

Property 'bar' does not exist on type 'Bar'

πŸ™‚ Expected behavior

bar.bar identified as Record<T, true> because it's in the same indexed property as bar.foo.type for T that is equals to f

@RyanCavanaugh
Copy link
Member

See #18758

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 20, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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