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

type transformation for mapping object properties to function parameters #12265

Closed
zpdDG4gta8XKpMCd opened this issue Nov 15, 2016 · 8 comments
Closed
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented Nov 15, 2016

now that we have map types it would nice being able to map properties to function parameters

type Constructor<T> = (P in T) => T;
interface Data {
    name: string;
    value: number;
}
type CreateData = Constructor<Data>;

would desugar to

type CreateData = (name: string, value: number) => Data;
@gcnew
Copy link
Contributor

gcnew commented Nov 15, 2016

I can see this, paired with substraction types, being used for

  • ADT constructors
  • bind / apply

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Nov 15, 2016
@aluanhaddad
Copy link
Contributor

This is really interesting

@dsherret
Copy link
Contributor

dsherret commented Nov 16, 2016

What determines the order of the parameters?

// somefile.ts
interface Data {
    otherProperty: string;
}

// otherfile.ts
interface Data {
    name: string;
    value: number;
}

// mainfile.ts
type Constructor<T> = (P in T) => T;
type CreateData = Constructor<Data>; // ?

@zpdDG4gta8XKpMCd
Copy link
Author

the order of declaration:

interface One {
   one: number;
}
interface Two extends One {
   two: string;
}
interface Three {
   three: boolean;
}
interface Four extends Three, Two {
}
type MakeFour = Contructor<Four>;
// same as
type MakeFour = (three: boolean, one: number, two: string) => Four

@zpdDG4gta8XKpMCd
Copy link
Author

in you case it should be a type error since the order cannot be determined

@weswigham
Copy link
Member

Why not just make object spread types valid in argument positions (so the spread type flattens into part of the argument list)? I think that would solve this elegantly:

type Constructor<T> = (...T) => T;
interface Data {
    name: string;
    value: number;
}
type CreateData = Constructor<Data>;

It also makes it obvious how to combine a spread object with normal arguments, and removes a confusingly useless type parameter.

@zpdDG4gta8XKpMCd
Copy link
Author

why not?

because using (...T) you can only do trivial mapping (property of some type to a parameter of exactly the same type), sure it will support this exact use case, but extrapolating the ideas of mapped type we might support more sophisticated use cases:

type Crazify<T> = (P in keyof T: MyCrazyParam<T[P]>) => MyEvenCrazierType<T>;

@RyanCavanaugh
Copy link
Member

I don't know why we didn't decline this earlier. We can't take a reliance on property ordering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants