Skip to content

Omit<> excludes types from merged interfaces including index types #40999

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
Kapcash opened this issue Oct 8, 2020 · 3 comments
Closed

Omit<> excludes types from merged interfaces including index types #40999

Kapcash opened this issue Oct 8, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Kapcash
Copy link

Kapcash commented Oct 8, 2020

TypeScript Version: v4.1.0-dev.20201008

Search Terms: Omit merged interface, Generic type with declaration merging

Code

interface Todo extends Record<string, any> {
  title: string
  description: string
  completed: boolean
}

interface Todo extends Record<string, any> {
  tags: string[]
}

const todo: Omit<Todo, 'description'> = {
  title: 'Todo 1',
  completed: false,
  tags: [''],
}

const trimedTags = todo.tags.map(tag => tag.trim()) // tags is considered as 'any' here.

Expected behavior:
Using Omit<> on types defining index types ([key: xxx]), the types from merged interface should remain defined and not fallback to the specified index type.

Actual behavior:
Any types from the merged interface are transformed to 'any' or 'unknown', as if they were only part of the index type, but they are actually defined. I expect them to keep their defined type from the merged interface.

It works well using Pick<> though.

Playground Link:

Here is a playground showing the error.

Related Issues:
Indexed access operator issue with Omit type #36316

@andrewbranch
Copy link
Member

Duplicate of #31153@dragomirtitian's OmitFromKnownKeys suggestion solves your problem.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Oct 9, 2020
@Kapcash
Copy link
Author

Kapcash commented Oct 10, 2020

Got it, thanks!
And sorry for the duplicate, I wish I found the issue myself.

@Kapcash Kapcash closed this as completed Oct 10, 2020
@andrewbranch
Copy link
Member

No worries, duplicates can be hard to find in a haystack of 40k 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants