-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Mapped type should take a function form as well #21421
Comments
Someone suggested to do: type Functionize <T> = {
[P in keyof T]: () => T[P];
}; however it does not work with extra function parameters, e.g. type Functionize <T> = {
[P in keyof T]: (values: T[]) => T[P];
}; compiler have no errors when values argument is not defined in the implementor of Functionize interface. EDIT: it appears to work partially, if I define lets say |
I believe the issue is that a signature that accepts fewer inputs is assignable to one that accepts more (provided they agree on matching parameters and output). So the following is acceptable: let f: () => number = () => 42;
let g: (x: number[]) => number = f; In your specific example, the type You get a compiler error when using Also, from your requirements I think the definition of type Functionize <T> = {
[P in keyof T]: (values: (T[P])[]) => T[P]; // or [P in keyof T]?: (values: (T[P])[]) => T[P] if you want optional properties
}; (added a lookup on the type of |
correct, sorry Im using a bit different code, I just wanted to provide an example and make this mistake.
correct, that's exactly issue I have. Is it tracked, or is it by design? |
I believe it's by design. From the spec:
when defining whether call-signature N is a subtype of call-signature M. Intuitively if a user writes a function of type The only way I could see this being something you don't want is if the output of the function must come from the input of the function. So in the type:
the output of
The rules about adding extra inputs to a function signature still apply, but in this case it's impossible to create something of type |
here is the syntax for definitnon a mapped type with function typed properties: type Funcs<T> = {[P in keyof T]?: (entities: P[], someBoolean: boolean) => T }; |
okay thank you guys, I think this issue can be closed. |
According to #12114 mapped types currently support following forms:
I think it shall also at least support a function form:
Currently Im trying to implement a
Functionize<T>
interface which forces implementors to implement any property of the T, but make it a function with maybe additional arguments. Example:I want to do Functionize which I want to give me:
And I'm asking about following method signature:
The text was updated successfully, but these errors were encountered: