Skip to content

Record<string, never> branch is not recognized on condition over object  #47071

Closed
@javiertury

Description

@javiertury

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions