-
Notifications
You must be signed in to change notification settings - Fork 22
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
Real world usage of omit is now awkward #108
Comments
There is also a confusing type error generated which suggests that both |
Reviewing your TS Playground, the types for Let's examine it: import R from "ramda";
export interface Params {
foo: string;
bar: string;
}
export function fn1(f: typeof fn2) {
return (params: Omit<Params, "bar">) => {
return f(R.omit(["bar"] as const, params));
};
}
function fn2({ foo, bar }: Params): boolean {
return false;
}
const a: Params = {foo: "foo", bar: "bar"};
fn1(fn2)(a); The error you're getting is in export function fn1(f: typeof fn2) {
return (params: Omit<Params, "bar">) => {
return f(R.omit(["bar"] as const, params)); // Error: see below
};
} The error:
Honestly, this is fucking weird. Why is complaining about This segways me into part 2 here. The typings for
export function fn1(f: typeof fn2) {
return (params: Params) => {
return f(R.omit(["bar"] as const, params)); // still Error, but better
};
} The error:
Now we get the error I'm expecting, with no mention of Why are we getting this error? That's simple. function fn2({ foo, bar }: Params): boolean {
return false;
}
fn2({ foo: '' });
// ^^ Error:
Argument of type '{ foo: string; }' is not assignable to parameter of type 'Params'.
Property 'bar' is missing in type '{ foo: string; }' but required in type 'Params'. Without this error, you may get a runtime error when trying to access a method on You mentioned unit tests. If your goal is to test how the runtime behavior deals with missing props, then casting the type in your test is the appropriate solution to get around this type guard error export function fn1(f: typeof fn2) {
return (params: Params) => {
// test `f` is it unexpectedly receives `Omit<Params, 'bar'>` instead of `Params`
return f(R.omit(["bar"] as const, params) as Params);
};
} If at runtime the prop bar is generally expected to be possibly type Params {
foo: string;
bar?: string // now optional
}; I'm making a lot of assumption based solely off that simple playground example, as well as how you mentioned Unit tests. So I'm hoping I'm not way off base with my assertions against your issues. Please let me know if I am and we can work on making some better examples in the TS Playground to see where the changes to the types for |
For me, the argument on why type Foo = {x: string, y: string}
type WithoutZ = Omit<Foo, 'z'> // this works just fine
const foo = {x: "this is X", y: "this is Y"}
R.omit('z', {foo: 'value'}) // this should work too and the return value should be equivalent to WithoutZ TypeScript types are loose, so the objects that match a type with keys type Foo = {x: string, y: string}
const fooInstance = // data fetched from somewhere else, parsed using zod, manipulated along a tranformation pipeline, you name it
function sendFooToExternalService(foo: Foo) {
send(R.omit('someKeyThatMightHaveBeenAddedAlongTheWay', foo))
} The argument for not accepting unknown keys is to avoid typos. Maybe I really meant to omit
|
IMHO It's better to be strict and allow you to opt-out of that strictness, than remove it altogether. You can always get around this by |
It did occur to me that |
I think the types of omit have been strengthened recently as (since minor upgrades to v0.29) I have a number of type errors that are flagged, especially for omit.
If you have a function signature, you may omit certain object keys in the sense, "I'm only interested in a subtype". This allows, say, unit tests to be more minimal and aligns with the principle of least knowledge. But in usage, the omitted keys may be present and you may want to acknowledge this in the implementation.
I'm not sure this is the most minimal example, but I hope shows what I mean:
See also TS Playground link
In other words, I would expect seemingly unrelated keys not to be a type error. I can probably work around this by creating my own omit implementation, but this goes against the spirit of ramda being a useful multi-tool library when I can't actually use it in a multitude of cases.
I can already hear gnashing of teeth, because probably someone has worked hard to add the new types. Of course, I can see the opposite argument that the strong typing will flag up potential typos.
The text was updated successfully, but these errors were encountered: