-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 seems broken on type extending Record
#50638
Comments
Duplicate of #31153 |
Thanks for finding that! My search terms didn't surface it. The issue seems to be closed, even though I don't see a clean working solution under the current version of TypeScript. It still seems to me that this is a bug with Happy to close this if it is viewed as a duplicate and resolved, but #31153 didn't have a reason for being marked as completed, from what I can see, just a reason for why the behaviour is the way it is. |
It's working as intended, not a bug. The linked issue provides a working workaround. The TypeScript team won't introduce new implementations for
Because |
Thanks for the response, I'm closing this issue then :) Understood about it working as intended, and the reasoning about not giving a breaking change. As a user, and a reader of the docs, I have to say this doesn't seem desirable, even if it's not a bug. This seems to non-insignificantly decrease the usability of the
I appreciate that, that's what I meant by "a reason for why the behaviour is the way it is". I understand how it behaves the way it does, but that seems to be a description of an implementation detail, not a description of why "Omit" should do that. It seems desirable for "because it would break existing usages" somewhat makes sense to me if that is the decision the TypeScript team has made. |
I'm not sure what do you mean by "a clean working solution" but if you are talking about the issue mentioned in #31153 (comment), it could be improved by using a util type. See my latest comment in that post. |
To be fair, I think the entire data structure you propose is undesirable. It's unsound and can lead to hard-to-find bugs. For example: const data: Foo = { bar: '', baz: 0 };
const key = "bar" as string;
data[key] = true;
data.bar; // Type is string, actually a boolean. |
That is an awful lot of code to be able to remove a single key from a type. To be clear I'm not ungrateful, I appreciate there being a solution. It just seems like that shouldn't be necessary when it seems like that's what
That's entirely valid, but to be clear my choice of type Foo = {
bar: string;
baz: number;
} & Record<string, number | string>
type OmitFoo = Omit<Foo, "baz"> Still demonstrates what I believe to be undesirable behaviour, and isn't unsound (if I understand your critique correctly) |
Changes nothing, really. As soon as you have a union type you can end up with unsound behaviour. |
Imho every human reading that code in issue description would expect same result as the one asked, is pretty intuitive and there is no ambiguity in it. It should omit from known members and other kept untouched. |
Bug Report
π Search Terms
omit, extend, Record, &
π Version & Regression Information
β― Playground Link
Playground link on nightly with relevant code
π» Code
π Actual behavior
Both
FooTest
andFoo2Test
resolve as being:π Expected behavior
I would expect the resolved type to be equivalent to:
The text was updated successfully, but these errors were encountered: