Repository navigation
type transformation for mapping object properties to function parameters #12265
Description
Activity
I can see this, paired with substraction types, being used for
- ADT constructors
bind/apply
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptIn DiscussionNot yet reached consensusNot yet reached consensus
on Nov 15, 2016 aluanhaddad commented
on Nov 16, 2016 ContributorMore actionsThis is really interesting
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 commented
on Nov 16, 2016 AuthorMore actionsthe 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) => FourzpdDG4gta8XKpMCd commented
on Nov 16, 2016 AuthorMore actionsin you case it should be a type error since the order cannot be determined
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.
Reacted by Tobias Cudnik, kiara and Leon AdlerzpdDG4gta8XKpMCd commented
on Nov 20, 2016 AuthorMore actionswhy 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 commented
on Jul 21, 2020 MemberMore actionsI don't know why we didn't decline this earlier. We can't take a reliance on property ordering.
Reacted by Wesley Wigham and Andrii Kirmas
now that we have map types it would nice being able to map properties to function parameters
would desugar to