Closed
Description
Bug Report
🔎 Search Terms
Record<string, never>
object condition
Record<string, never> branch is not recognized on condition over object
🕗 Version & Regression Information
- This is the behavior in every version I tried, from 3.3.3 to 4.5.2 and nightly
⏯ Playground Link
Playground link with relevant code
💻 Code
type Config <T extends object> =
& { mandatory: number }
& (T extends Record<string, never>
? { custom?: undefined }
: { custom: T });
const fn = <T extends object = Record<string, never>>(config: Config<T>) => {
const custom = config.custom; // Wrong, typescript is not aware that it can be undefined
// Do something
console.log(Object.keys(custom));
}
fn({ mandatory: 1 }) // Broken, `config.custom` can be undefined
fn({ mandatory: 1, custom: { a: 'a' } }) // Works
🙁 Actual behavior
Typescript says that the variable const custom
inside fn()
is of type object
, but it's not true, it can be undefined. Running the code throws an error, even though typescript doesn't see anything wrong with code.
🙂 Expected behavior
T variable const custom
inside fn()
should be of type object | undefined
.