Skip to content

T wider in lazy branch of T | (() => T) for known T #55277

Closed
@pckilgore

Description

@pckilgore

🔎 Search Terms

T | (() => T)
"T | (() => T)" inference
inference of lazy generic type
lazy generic inferred too wide

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about callbacks and functions and laziness and sum types.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBA8mwEsD2A7AhgGwyAMmgXiADwAqAfFALxQlQA+UAFIwJRUUksBQokUAsiBLho1AN5Q0ALigAjJEgwQ0KKAF8uAY1QBnYFABmq6o2Ay4iVJmx5CRQcMhk2lCmI1cjjCdKjAATgCuEAA0UDoAFkiBGAAmAGJoCBgyAIxqbAD0mVAAIhA6CP5oskqeKMwuFN6SMgHBYZHRcQZJKVAA5MARCDpQsUgFUChI+q3JYQDuPZoRUL1yaLEd6ixZOQBCS0A

💻 Code

type OptionallyLazy<T> = T | (() => T)
type MyType = { a: boolean }
const fn = (t: OptionallyLazy<MyType>) => {}

fn({ a: true, shouldFail: 'it does, good'})
fn(() => ({ a: true, shouldFail: 'this does not fail, which is bad' }))

🙁 Actual behavior

No type error for shouldFail on last line above.

🙂 Expected behavior

Type error for shouldFail on last line above, same as line above it.

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