Curiosity, what's the internal type Ensure<T>
for?
#949
-
So, I was looking into TypeBox' implementation, and I found this type, used as a sort of wrapper when constructing type Ensure<T> = T extends infer U ? U : never and I'm still wondering what's this for 😅 . Anyone who knows what's its purpose? (My naive understanding only sees a type wrapper that leaves |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@castarco Hi, TypeBox has two internal static evaluation types. Evaluate and Ensure. Ill try explain them a bit below. EvaluateThe Evaluate type (sometimes called Prettify) is used to produce a clean object type, usually after intersection. TypeBox uses the term Evaluate because the mapping actually causes TypeScript to evaluate a type as a normalized distributed union. Consider the following. import { Evaluate } from '@sinclair/typebox'
type T = Evaluate<({ // type T = {
a: number // a: number;
} | { // c: number;
b: number // } | {
}) & { // b: number;
c: number // c: number;
}> // } EnsureThe Ensure type is similar to Evaluate in that it is used to produce a clean output type. It's used in cases where TS returns the mapping alias type name, but where the result of that mapping is preferred. type Ensure<T> = T extends infer U ? U : never
// ^ don't use T, used unnamed inferred U It's a somewhat difficult to demonstrate Ensure without context. Consider the following. import { Ensure, TSchema, Static, Type } from '@sinclair/typebox'
const A = Constructor1([Type.String()], Type.String()) // const A: StaticConstructor1<[TString], TString, []>
const B = Constructor2([Type.String()], Type.String()) // const B: new (param_0: string) => string
// ----------------------------------------------------------------------------------
// Mappings
// ----------------------------------------------------------------------------------
type StaticReturnType<U extends TSchema, P extends unknown[]> = Static<U, P>
type StaticParameter<T extends TSchema, P extends unknown[]> = [Static<T, P>]
type StaticParameters<T extends TSchema[], P extends unknown[], Acc extends unknown[] = []> = (
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? StaticParameters<R, P, [...Acc, ...StaticParameter<L, P>]>
: Acc
)
type StaticConstructor1<T extends TSchema[], U extends TSchema, P extends unknown[]> =
new (...param: StaticParameters<T, P>) => StaticReturnType<U, P>
type StaticConstructor2<T extends TSchema[], U extends TSchema, P extends unknown[]> = Ensure<
new (...param: StaticParameters<T, P>) => StaticReturnType<U, P>
>
declare function Constructor1<T extends TSchema[], U extends TSchema>(T: [...T], U: U): StaticConstructor1<T, U, []>
declare function Constructor2<T extends TSchema[], U extends TSchema>(T: [...T], U: U): StaticConstructor2<T, U, []> Hope this helps :) |
Beta Was this translation helpful? Give feedback.
@castarco Hi,
TypeBox has two internal static evaluation types. Evaluate and Ensure. Ill try explain them a bit below.
Evaluate
The Evaluate type (sometimes called Prettify) is used to produce a clean object type, usually after intersection. TypeBox uses the term Evaluate because the mapping actually causes TypeScript to evaluate a type as a normalized distributed union. Consider the following.
TypeScript Link Here