ValueOf<Type>
constructs a type for type Type
where it equals to:
- The primitive for a primitive (as just using
Type
)
type BooleanValue = ValueOf<boolean>;
// ^? boolean
type StringValue = ValueOf<string>;
// ^? string
- The array elements for an array (like
ElementOf<Type>
)
const array = [false, 1, "2"];
const tuple = asTuple(["<first name>", "<last name>"]);
type ArrayValue = ValueOf<typeof array>;
// ^? string | number | boolean
type TupleValue = ValueOf<typeof tuple>;
// ^? string
- The function return type for a function (like
ReturnType<Type>
)
const asTuple = <Type extends Tuple>(args: Type): Type => args;
const identity = <Type>(value: Type) => value;
type AsTupleFunctionValue = ValueOf<typeof asTuple>;
// ^? [any?, ...any[]]
type IdentityFunctionValue = ValueOf<typeof identity>;
// ^? unknown
- The object property values for objects
interface User {
id: number;
name: string;
email: string;
}
type UserValue = ValueOf<User>;
// ^? string | number
- When function has generic type without a default value type, it returns
unknown
(e.g.identity
) - When function has generic type with a default value type, it returns this default value type (e.g.
asTuple
)
TS Playground – https://tsplay.dev/N55YMN