Is it possible to return generic type from argument passed to a function
Date : March 29 2020, 07:55 AM
hop of those help? Use can use a conditional type to extract the type argument from EVENT. Type parameters are not inferred one based on another usually, and you end up with the narrowest possible type (in this case {}) abstract class BaseEvent<REQUEST_PAYLOAD, RESPONSE_PAYLOAD> {
constructor(public requestPayload: REQUEST_PAYLOAD) { }
// this method was only created for the sake of showing response1 type example
public return_RESPONSE_PAYLOAD_type(): RESPONSE_PAYLOAD {
return null;
}
}
type GetUserInfoRequest = { userId: number };
type GetUserInfoResponse = { username: string; age: number };
class GetUserInfoEvent extends BaseEvent<
GetUserInfoRequest,
GetUserInfoResponse
> { }
// Conditional type to extract the response payload type:
type RESPONSE_PAYLOAD<T extends BaseEvent<any, any>> = T extends BaseEvent<any, infer U> ? U : never;
const emit = async <
EVENT extends BaseEvent<any, any>
>(
event: EVENT
): Promise<RESPONSE_PAYLOAD<EVENT>> => {
// some stuff will be done there - for the sake of example it was removed
return null;
// return event.return_RESPONSE_PAYLOAD_type(); // doesn't work aswell
};
const main = async () => {
const event = new GetUserInfoEvent({ userId: 666 });
const response1 = event.return_RESPONSE_PAYLOAD_type(); // type === { username: string; age: number; }
const response2 = await emit(event); // is now GetUserInfoResponse
response2.username //<-- ok
response2.age //<-- ok
};
|
How do you provide a default argument for a generic function with type constraints?
Date : March 29 2020, 07:55 AM
With these it helps The significant constraint is T: ExpressibleByStringLiteral. That's what allows something to be initialized from a string literal. func doSomething<T: Collection>(value: T = "abc")
where T.Element == Character, T: ExpressibleByStringLiteral {
// ...
}
func doSomething<T: StringProtocol>(value: T) {
}
func doSomething() {
doSomething(value: "abc")
}
|
Cannot convert return expression of generic protocol's function type implementation
Date : March 29 2020, 07:55 AM
|
How to get correct return type depending on the generic argument from a generic function
Date : March 29 2020, 07:55 AM
I hope this helps . TypeScript currently lacks support in the type system for most higher kinded types, which is what you'd need to represent an operation on a generic function type. So the short answer is "you can't do this, sorry". type T1 = { a: number };
const t1 = true as false || f1(null! as number);
type T1 = typeof t1; // {a: number}
type T1 = typeof f1(123);
|
Inferred generic function typechecks as a return type but not an argument type
Date : January 02 2021, 06:48 AM
this one helps. I'm learning about SYB and rank n types, and came across a confusing case of what seems like the monomorphism restriction. , In your second snippet, op is actually not polymorphic. shallowest p = let { op = (empty `mkQ` p) } in op
types values
↓ ↓
x, a, f, ...; op :: x -> f a, ... |- op :: x -> f a
↑
monotype (no "forall")
In English: "op has type (x -> f a) in the context consisting of type variables (x, a, f, ...) and values (op :: x -> f a, ...)"
x, a, f |- (let op = ... in op) :: x -> f a
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻ (generalization)
|- (let op = .... in op) :: forall x a f. x -> f a
|