Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kohver opened this issue Apr 28, 2021 · 2 comments
Closed

Comments

@kohver
Copy link

kohver commented Apr 28, 2021

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.

@MartinJohns
Copy link
Contributor

See #43478 and many others.

@kohver
Copy link
Author

kohver commented Apr 29, 2021

Thank you for the link. Closing the issue

@kohver kohver closed this as completed Apr 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants