Skip to content
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

Closed
wants to merge 2 commits into from
Closed

Add $ObjMap #38

wants to merge 2 commits into from

Conversation

malash
Copy link
Contributor

@malash malash commented Dec 17, 2018

type ObjMapper<T> = (value: $Values<Required<T>>, ...args: any[]) => any;

export type $ObjMap<T extends object, F extends ObjMapper<T>> = {
  [K in keyof T]: ReturnType<F>
};

$ObjMap will generate mapper function type from given object type.

For example if T is { a: number; b?: string } the mapper should be

(value: number | string, ...args: any[]) => any

Mapper doesn't need to handle undefined for optional properties so undefined is not included in value's type.

If mapper doesn't match TS will warn for it:

image

Must use --strict for tsc

Fix #18

@@ -1,5 +1,6 @@
{
"compilerOptions": {
"strict": true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -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;
Copy link
Owner

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

Copy link
Contributor Author

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;
Copy link
Owner

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?

Copy link
Contributor Author

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;

Copy link
Contributor Author

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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is the resulting type is not correct. It's falling back to any ({}), please take a look:
screen shot 2018-12-19 at 10 36 12 am

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two probleams.

  1. The Mapper failed because it doesn't match F extends _ObjMapper<T>. I don't knwon why and given a demo.

image

If you use F extends (...args: any[]) => any it will work:

image

  1. The return value should be {} ( as known as any ) because ReturnType<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
};

Copy link
Owner

@piotrwitek piotrwitek Dec 19, 2018

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.

@malash malash closed this Dec 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants