🔎 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.