-
-
Notifications
You must be signed in to change notification settings - Fork 230
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 $ObjMap #38
Add $ObjMap #38
Conversation
@@ -1,5 +1,6 @@ | |||
{ | |||
"compilerOptions": { | |||
"strict": true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
src/utility-types.ts
Outdated
@@ -58,3 +58,14 @@ export type $Call<Fn extends (...args: any[]) => any> = Fn extends ( | |||
) => infer RT | |||
? RT | |||
: never; | |||
|
|||
type ObjMapper<T> = (value: $Values<Required<T>>, ...args: any[]) => any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For helper aliases convention is "_" prefix and @Private jsdoc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, Fixed.
b?: boolean; | ||
c: string | null; | ||
}; | ||
type Mapper = (value: number | boolean | string | null) => string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks interesting, but I would really like to solve below case with this type:
type Obj = {
a: () => number;
b: () => boolean;
c: () => string | null;
};
type Mapper = <T>(value: () => T) => T; // the whole point is to use generics in the mapper, but Flow inference works different than in TS
type MappedObj = $ObjMap<Obj, Mapper>;
// type MappedObj = {
// a: number
// b: boolean
// c: string|null
// }
Currently it doesn't work and I don't see a way to do that. Any ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried this line and it works:
type Mapper = <T>(value: () => T extends any ? T : never) => T;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I make a bad case demo:
const f = {} as <T>(value: () => T) => void;
const f1: (value: (() => number) | (() => boolean)) => void = f; // this line failed check
const f2: (value: (() => number | boolean)) => void = f;
But I'm not sure why it happened.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two probleams.
- The
Mapper
failed because it doesn't matchF extends _ObjMapper<T>
. I don't knwon why and given a demo.
If you use F extends (...args: any[]) => any
it will work:
- The return value should be
{}
( as known asany
) becauseReturnType<Mapper>
returns{}
in current implement.
There is no $Call
in TypeScript ( the one in this repo is not fully equal to Flow Type's ). Consider this Flow Type example:
type Mapper = <T>(value: () => T) => T;
type Result = $Call<Mapper, () => number>; // Result is number
const result: Result = 1;
$Call
can infer T
because value
is () => number
but not TypeScript.
Otherwise you can refactor $ObjMap
to:
export type $ObjMap<T extends object, F extends _ObjMapper<T>> = {
[K in keyof T]: $Call<F, T[K]>; // doesn't work yet
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said I don't see any way that would work, that's why this PR doesn't make sense. It's basically useless because it doesn't solve the primary use case and is also confusing because people will try to use it and complain it doesn't work in the same way I just did.
I'm afraid I have to reject this.
$ObjMap
will generate mapper function type from given object type.For example if
T
is{ a: number; b?: string }
the mapper should beIf mapper doesn't match TS will warn for it:
Fix #18