Skip to content
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

Add Omit as a known interface #30455

Closed
5 tasks done
basarat opened this issue Mar 18, 2019 · 14 comments · Fixed by #30552
Closed
5 tasks done

Add Omit as a known interface #30455

basarat opened this issue Mar 18, 2019 · 14 comments · Fixed by #30552
Assignees
Labels
Breaking Change Would introduce errors in existing code Committed The team has roadmapped this issue Fixed A PR has been merged for this issue In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@basarat
Copy link
Contributor

basarat commented Mar 18, 2019

Search Terms

Omit

Suggestion

Add the following type to lib.d.ts:

type Omit<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>;

More

Origin : #12215 (comment)

From : https://github.com/sindresorhus/type-fest/blob/0226ef2092ee2b07a8714c832d106da659877a8f/index.d.ts#L75 Please open new issues on TypeScript about making it built-in.. I searched but couldn't find an issue, so here it is 🌹

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@j-oliveras
Copy link
Contributor

Duplicate of #19569?

@weswigham
Copy link
Member

Not really? That was back when there was only a hairy mapped-type based definition of Omit which we didn't particularly like, so didn't want to add.

@nmain
Copy link

nmain commented Mar 19, 2019

Eventually that issue was closed after conditional types were added. @ahejlsberg made #21847 and said:

We did not include the Omit<T, K> type because it is trivially written as Pick<T, Exclude<keyof T, K>>

@dragomirtitian
Copy link
Contributor

@nmain My 2¢, trivial for some magic for others, I would argue all build-in mapped and conditional types are trivial to write but that does not make them any less useful. Having a common set of types makes code easier to understand across projects. Omit is also simpler to understand for newer typescript devs and they don't have to do conditional type math when looking at a definition, they just care about the behavior of the type.

@sindresorhus
Copy link

I personally use Omit much more often than the other built-in helper types.

@jeremiergz
Copy link

Totally agree, I end up declaring that Omit type in almost every project.
It's like Record<string, string> could be written { [k: string]: string}. I prefer the Record helper, same feeling about this one.

@SleeplessByte
Copy link

I've used Omit in every single project and every single time I have to look it up, because Pick<T, Exclude<keyof T, K>> isn't trivial to recall; if many people use it in every project, it make sense to make it part of lib.d.ts right?

It's like Record<string, string> could be written { [k: string]: string}. I prefer the Record helper, same feeling about this one.

That's not the same; and it's not even close.

@jeremiergz
Copy link

That's not the same; and it's not even close.

How so? Isn't it better to explain your train of thought when you say someone's wrong?...

@SleeplessByte
Copy link

SleeplessByte commented Mar 21, 2019

That's not the same; and it's not even close.

How so? Isn't it better to explain your train of thought when you say someone's wrong?...

Sorry, you're right -- and my thoughts were wrong anyway. I would even say that omit is far more difficult than record!

What I should have said is that it's better to compare Record<keyof K, string> to { [P in K]: string }.

@fabiospampinato
Copy link

The docs' reasoning about this is:

We did not include the Omit<T, K> type because it is trivially written as Pick<T, Exclude<keyof T, K>>

I'm not so sure about the triviality of it, especially for new TS devs, but IMHO this isn't a good reason for not including Omit, to me it feels like saying that if I have this code:

return collection.map ( item => item.foo () );

I don't really need map because it's trivial to do the same with forEach:

const result = [];
collection.forEach ( item => result.push ( item.foo () ) );
return result;

I mean yeah, it's trivial, but am I supposed to write such ugly code every time or am I supposed to redefine map every time?

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Mar 25, 2019
@DanielRosenwasser DanielRosenwasser added the Committed The team has roadmapped this issue label Apr 4, 2019
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.5.0 milestone Apr 4, 2019
@DanielRosenwasser DanielRosenwasser self-assigned this Apr 4, 2019
@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Apr 4, 2019
@DanielRosenwasser
Copy link
Member

This is now 3.5-bound! 🚀

You can try it out with our next nightly release.

@DanielRosenwasser DanielRosenwasser added the Breaking Change Would introduce errors in existing code label Apr 5, 2019
@leidegre
Copy link

leidegre commented Apr 7, 2019

For those curious how the Omit type is used, you can find that information here.

@NoelAbrahams
Copy link

The naming of this is rather unfortunate. I mean to say, what exactly is the difference between the words "Omit" and "Exclude"? They are essentially synonyms with a minor nuance.

The definition we have used is ExcludeProperty.

/**
 * Exclude from T those types that are assignable to U
 */
type Exclude<T, U> = T extends U ? never : T;

/**
 * From T remove the properties K
 * E.g. ExcludeProperty<{ a: number, b: string }, 'b'> => { a: number }
 */
type ExcludeProperty<T, K extends keyof T> = Pick<T, Exclude<StringKeyOf<T>, K>>;

@sindresorhus
Copy link

For everyone watching this issue, note that the Omit type that was added is not the strict one suggested here. See: #30825.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change Would introduce errors in existing code Committed The team has roadmapped this issue Fixed A PR has been merged for this issue In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.