Repository navigation
Closed
Labels
Breaking ChangeWould introduce errors in existing codeWould introduce errors in existing codeCommittedThe team has roadmapped this issueThe team has roadmapped this issueFixedA PR has been merged for this issueA PR has been merged for this issueIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
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.
Reacted by Titian Cernicova-Dragomir, NN, Mali McCalla, Christian, Jarek Radosz, ulrichb, Will Chen, Sindre Sorhus, Sam Verschueren, Braden Snell and 169 more
Reacted by ulrichb, Mark Dalgleish, Jiri Spac, Sasha Koss, wpq0dev, Veniamin Krol, Steven, Ernesto García, Caleb Eby, Derk-Jan Karrenbeld and 18 more
Reacted by Sasha Koss, Veniamin Krol, Steven, Jefry Dewangga, Pascal Iske, cevek, Kevin Rambaud, a1300, Yahor Siarheyenka, Pubudu Dodangoda and 4 more
Metadata
Metadata
Labels
Breaking ChangeWould introduce errors in existing codeWould introduce errors in existing codeCommittedThe team has roadmapped this issueThe team has roadmapped this issueFixedA PR has been merged for this issueA PR has been merged for this issueIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
Activity
j-oliveras commentedon Mar 18, 2019
Duplicate of #19569?
weswigham commentedon Mar 18, 2019
Not really? That was back when there was only a hairy mapped-type based definition of
Omitwhich we didn't particularly like, so didn't want to add.nmain commentedon Mar 19, 2019
Eventually that issue was closed after conditional types were added. Anders Hejlsberg (@ahejlsberg) made #21847 and said:
dragomirtitian commentedon Mar 19, 2019
Nicholai Main (@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.
Omitis 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 commentedon Mar 20, 2019
I personally use
Omitmuch more often than the other built-in helper types.jeremiergz commentedon Mar 21, 2019
Totally agree, I end up declaring that
Omittype in almost every project.It's like
Record<string, string>could be written{ [k: string]: string}. I prefer theRecordhelper, same feeling about this one.SleeplessByte commentedon Mar 21, 2019
I've used
Omitin every single project and every single time I have to look it up, becausePick<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 oflib.d.tsright?That's not the same; and it's not even close.
jeremiergz commentedon Mar 21, 2019
How so? Isn't it better to explain your train of thought when you say someone's wrong?...
SleeplessByte commentedon Mar 21, 2019
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 commentedon Mar 22, 2019
The docs' reasoning about this is:
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:
I don't really need
mapbecause it's trivial to do the same withforEach:I mean yeah, it's trivial, but am I supposed to write such ugly code every time or am I supposed to redefine
mapevery time?DanielRosenwasser commentedon Apr 4, 2019
This is now 3.5-bound! 🚀
You can try it out with our next nightly release.
leidegre commentedon Apr 7, 2019
For those curious how the
Omittype is used, you can find that information here.NoelAbrahams commentedon Apr 9, 2019
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.sindresorhus commentedon Apr 10, 2019
For everyone watching this issue, note that the
Omittype that was added is not the strict one suggested here. See: #30825.