ReturnType
Like the builtin helper but more convenient to use.
If you have a function like:
type Shape = {};
async function getShape(): Promise<Shape> {
return {};
}
And you are trying to extract
Shape
, when you use the default return type:type shape = ReturnType<typeof getShape>;
// ^ shape is Promise<Shape> 😤
With
tsafe
's ReturnTypeimport type { ReturnType } from "tsafe";
type shape = ReturnType<typeof getShape>;
// ^ shape is Shape 😊
Let's say we have an interface defined as such:
export type Api = {
getShape?: () => Shape;
};
And we want to extract the type
Shape
, using the default ReturnType
we have to do:type shape = ReturnType<NonNullable<Api["getShape"]>>;
With the ReturnType of
tsafe
you don't need NonNullable
import type { ReturnType } from "tsafe";
type shape = ReturnType<Api["getShape"]>;
Last modified 8mo ago