-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Omit outputting incorrect types for types with mixed [key: string]: string | undefined
and defined field types
#49656
Comments
Oh right, ok. Would adding this
as a type in my codebase be the best course of action in this case? It seems to preserve the type of the fields. In fact, if this preserves the types of the fields, why is this not how Omit is coded? Would be interested to know why this is desired behaviour. |
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>; It's generally pretty hard to work with types that have index signatures along with their declared types; I would rewrite this as interface Address {
line1: string;
line2: string;
postcode: string
}
interface Parent {
id: string;
party: boolean;
address: Omit<Address, 'line2'> & { [key: string]: string | undefined };
} |
@RyanCavanaugh This is unfortunately a more legacy codebase and I can't remove the index signature without a lot of work elsewhere. |
I understand that, but if it can do that and do better for the user, what's the downside with defining it as I did above with edit: Please understand I'm asking this from a position of ignorance, not a position of 'I think I could do better' :) dont want you thinking just because I've put an issue in that I know better than the entire TS team 😂 |
Ah, good question.
|
Aha, that makes sense! Is there a way, then, that I can propose this as an upgrade to |
We don't issue large breaking changes, regardless of version numbers, unless there's a very identifiable concrete upside. I don't think tweaking |
That’s unfortunate - how would I go about overriding Omit with this improved version then?
Thanks,
James
…Sent from my iPhone
On 23 Jun 2022, at 18:14, Ryan Cavanaugh ***@***.***> wrote:
We don't issue large breaking changes, regardless of version numbers, unless there's a very identifiable concrete upside. I don't think tweaking Omit for this scenario would meet that bar.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.
|
|
I see - is there no way to override it in a namespace like you can with other types in libraries?
…Sent from my iPhone
On 23 Jun 2022, at 20:19, Ryan Cavanaugh ***@***.***> wrote:
--noLib + provide your own copies of the lib files
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.
|
You can lexically shadow it (including at module scope), but you can't globally clobber it |
So I’d need to define/import the type override in every file where I want it?
…Sent from my iPhone
On 23 Jun 2022, at 22:54, Ryan Cavanaugh ***@***.***> wrote:
You can lexically shadow it (including at module scope), but you can't globally clobber it
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.
|
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
Using
Omit
and[key: string]: string | undefined
together can result in Typescript not preserving the types of fields on the original type. Playground link here to display the issue: playground link for issue hereThe issue is that the fields that were previously defined as strings, are now returning as
string | undefined
, even though there's no reason they should, type-wise. If you remove the[key: string]: string | undefined
the problem goes away, and typescript is obviously clever enough to know that the fieldsline1
,line2
andpostcode
are just of typestring
, notstring | undefined
.🔎 Search Terms
omit
,omit undefined
🕗 Version & Regression Information
⏯ Playground Link
Link given above
💻 Code
🙁 Actual behavior
Omit does not preserve the types of the named fields
🙂 Expected behavior
Omit should preserve the types of the named fields
The text was updated successfully, but these errors were encountered: