API reference / @evolu/common / Type / SetType

Interface: SetType<ElementType>

Defined in: packages/common/src/Type.ts:2487

SetType extends Type with an additional element property for reflection.

Extends

Type Parameters

Type Parameter
ElementType extends AnyType

Properties

PropertyModifierTypeDescriptionInherited fromDefined in
[EvoluTypeSymbol]readonlytrue-Type.[EvoluTypeSymbol]packages/common/src/Type.ts:353
~standardreadonlyProps<ReadonlySet<InferInput<ElementType>>, ReadonlySet<InferType<ElementType>>>The Standard Schema properties.Type.~standardpackages/common/src/Type.ts:4450
elementreadonlyElementType--packages/common/src/Type.ts:2495
ErrorpublicSetError<InferError<ElementType>>The specific error introduced by this Type. ### Example type StringError = typeof String.Error;Type.Errorpackages/common/src/Type.ts:221
Errorsreadonly| SetError<InferError<ElementType>> | SetError<InferParentError<ElementType>>### Example type StringParentErrors = typeof String.Errors;Type.Errorspackages/common/src/Type.ts:417
fromreadonly(value) => Result<ReadonlySet<InferType<ElementType>>, | SetError<InferError<ElementType>> | SetError<InferParentError<ElementType>>>Creates T from an Input value. This is useful when we have a typed value. from is a typed alias of fromUnknown.Type.frompackages/common/src/Type.ts:236
fromParentreadonly(value) => Result<ReadonlySet<InferType<ElementType>>, SetError<InferError<ElementType>>>Creates T from Parent type. This function skips parent Types validations when we have already partially validated value.Type.fromParentpackages/common/src/Type.ts:330
fromUnknownreadonly(value) => Result<ReadonlySet<InferType<ElementType>>, | SetError<InferError<ElementType>> | SetError<InferParentError<ElementType>>>Creates T from an unknown value. This is useful when a value is unknown.Type.fromUnknownpackages/common/src/Type.ts:322
InputpublicReadonlySet<InferInput<ElementType>>The type expected by from and fromUnknown. ### Example type StringInput = typeof String.Input;Type.Inputpackages/common/src/Type.ts:219
isreadonlyRefinement<unknown, ReadonlySet<InferType<ElementType>>>A type guard that checks whether an unknown value satisfies the Type. ### Example const value: unknown = "hello"; if (String.is(value)) { // TypeScript now knows valueis astring here. console.log("This is a valid string!"); } const strings: unknown[] = [1, "hello", true, "world"]; const filteredStrings = strings.filter(String.is); console.log(filteredStrings); // ["hello", "world"]Type.ispackages/common/src/Type.ts:351
namereadonly"Set"-Type.namepackages/common/src/Type.ts:227
orNullreadonly(value) => | ReadonlySet<InferType<ElementType>> | nullCreates T from an Input value, returning null if validation fails. This is a convenience method that combines from with getOrNull. When to use: - When you need to convert a validation result to a nullable value - When the error is not important and you just want the value or nothing ### Example // ✅ Good: Optional user input const age = PositiveInt.orNull(userInput); if (age != null) { console.log("Valid age:", age); } // ✅ Good: Default fallback const maxRetries = PositiveInt.orNull(config.retries) ?? 3; // ❌ Avoid: When you need to know why validation failed (use from instead) const result = PositiveInt.from(userInput); if (!result.ok) { console.error(formatPositiveError(result.error)); }Type.orNullpackages/common/src/Type.ts:315
orThrowreadonly(value) => ReadonlySetCreates T from an Input value, throwing an error if validation fails. Throws an Error with the Type validation error in its cause property, making it debuggable while avoiding the need for custom error messages. This is a convenience method that combines from with getOrThrow. When to use: - Configuration values that are guaranteed to be valid (e.g., hardcoded constants) - Application startup where failure should crash the program - As an alternative to assertions when the Type error in the thrown Error's cause provides sufficient debugging information - Test code with known valid inputs (when error message clarity is not critical; for better test error messages, use Vitest schemaMatching + assert with .is()) ### Example // ✅ Good: Known valid constant const maxRetries = PositiveInt.orThrow(3); // ✅ Good: App configuration that should crash on invalid values const appName = SimpleName.orThrow("MyApp"); // ✅ Good: Instead of assert when Type error is clear enough // Context makes it obvious: count increments from non-negative value const currentCount = counts.get(id) ?? 0; const newCount = PositiveInt.orThrow(currentCount + 1); // ✅ Good: Test setup with known valid values const testUser = User.orThrow({ name: "Alice", age: 30 }); // ❌ Avoid: User input (use from instead) const userAge = PositiveInt.orThrow(userInput); // Could crash! // ✅ Better: Handle user input gracefully const ageResult = PositiveInt.from(userInput); if (!ageResult.ok) { // Handle validation error }Type.orThrowpackages/common/src/Type.ts:284
ParentpublicReadonlySet<InferParent<ElementType>>The parent type. ### Example type StringParent = typeof String.Parent;Type.Parentpackages/common/src/Type.ts:223
ParentErrorpublicSetError<InferParentError<ElementType>>The parent's error. ### Example type StringParentError = typeof String.ParentError;Type.ParentErrorpackages/common/src/Type.ts:225
TypereadonlyReadonlySetThe type this Type resolves to. ### Example type String = typeof String.Type;Type.Typepackages/common/src/Type.ts:364

Was this page helpful?