Skip to content

Omit breaks intersection with an object type with [key: string]: unknown #43864

Closed
@kohver

Description

@kohver

Bug Report

⏯ Playground Link

Playground link with relevant code

💻 Code

type ModalProps = {
  excludedProp: () => void;
  someProp: () => void;
  [key: string]: unknown;
}
function Component(props: Omit<ModalProps, "excludedProp">) {
  console.log(props.excludedProp()); // Object is type of 'unknown'. This is ok.
  console.log(props.someProp()) // Object is type of 'unknown'. This is unexpected.
}

Workaround:

type ModalProps = {
  excludedProp: () => void;
  someProp: () => void;
  [key: string]: unknown;
}
declare var { excludedProp, ...Omit_ModalProps_excludedProp }: ModalProps;
function Component(props: typeof Omit_ModalProps_excludedProp) {
  console.log(props.excludedProp()); // Object is type of 'unknown'
  console.log(props.someProp()); // works!
}

Also, this works:

type ModalProps = {
  excludedProp: () => void;
  someProp: () => void;
}
function Component(props: Omit<ModalProps, "excludedProp">) {
  console.log(props.excludedProp()); // Object is type of 'unknown'
  console.log(props.someProp()) // works!
}
type ModalProps = {
  someProp: () => void;
  [key: string]: unknown;
}
function Component(props: ModalProps) {
  console.log(props.someProp()) // works!
}

🙁 Actual behavior

In the first example the props.someProp is unknown.

🙂 Expected behavior

In the first example the props.someProp is () => void.

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