MCPcopy Create free account
hub / github.com/arktypeio/arktype / Type

Interface Type

ark/type/variants/object.ts:51–267  ·  view source on GitHub ↗

@ts-ignore cast variance

Source from the content-addressed store, hash-verified

49
50/** @ts-ignore cast variance */
51interface Type<out t extends object = object, in out $ = {}>
52 extends BaseType<t, $> {
53 readonly(): t extends array ? ArrayType<{ readonly [i in keyof t]: t[i] }, $>
54 : Type<readonlyObjectOf<t>, $>
55
56 // a builtin whose keys aren't props, as an overload only one matches: a
57 // conditional return, here or on the one below, would be deferred for a
58 // receiver known only by its constraint, leaving the Type of its keys
59 // unusable, where a `this` that can't be decided falls through instead.
60 // that t is a builtin is tested within `this` too: as self's constraint, it
61 // costs a receiver that isn't one about 4000 instantiations on each call
62 keyof<self extends { t: object }>(
63 this: [self["t"]] extends [array | BuiltinObject] ?
64 isKeylessBuiltin<unbrandKeyofOperand<self["t"]>> extends true ?
65 self
66 : never
67 : never
68 ): ErrorType<writeUnsatisfiableExpressionError<"keyof">>
69 // t is read from the receiver: Type<object>'s keys are never, so returning
70 // arkKeyOf<t> would keep an array or Date Type, which TS relates to
71 // Type<object> structurally rather than by variance, from being assignable
72 keyof<o extends object>(this: {
73 t: o
74 }): instantiateType<arkKeyOf<unbrandKeyofOperand<o>>, $>
75 // a union of Types has a t per member, and inference would pick one of them
76 // for o, which the other members then fail. Indexing gives their union
77 keyof<self extends { t: object }>(
78 this: self
79 ): instantiateType<arkKeyOf<unbrandKeyofOperand<self["t"]>>, $>
80
81 /**
82 * #### the Type at a key, or at a path of up to three keys
83 *
84 * @example
85 * const User = type({ name: "string", address: { city: "string" } })
86 * // Type<string>
87 * const City = User.get("address", "city")
88 */
89 get<const k1 extends arkIndexableOf<t>, r = instantiateType<gotAt<t, k1>, $>>(
90 k1: k1 | type.cast<k1>
91 ): r extends infer _ ? _ : never
92 get<
93 const k1 extends arkIndexableOf<t>,
94 const k2 extends arkIndexableOf<definedAt<t, k1>>,
95 r = instantiateType<gotAt<definedAt<t, k1>, k2> | undefinedAt<t, k1>, $>
96 >(
97 k1: k1 | type.cast<k1>,
98 k2: k2 | type.cast<k2>
99 ): r extends infer _ ? _ : never
100 get<
101 const k1 extends arkIndexableOf<t>,
102 const k2 extends arkIndexableOf<definedAt<t, k1>>,
103 const k3 extends arkIndexableOf<definedAt<definedAt<t, k1>, k2>>,
104 r = instantiateType<
105 | gotAt<definedAt<definedAt<t, k1>, k2>, k3>
106 | undefinedAt<definedAt<t, k1>, k2>
107 | undefinedAt<t, k1>,
108 $

Callers 70

shapes.tsFile · 0.65
generic.test.tsFile · 0.65
brand.test.tsFile · 0.65
cast.test.tsFile · 0.65
mapped.test.tsFile · 0.65
props.test.tsFile · 0.65
consumer.tsFile · 0.65
parseKeyOfTupleFunction · 0.65
applyPrefixesMethod · 0.65
brand.test.tsFile · 0.65
deferred.test.tsFile · 0.65
keyof.test.tsFile · 0.65

Calls

no outgoing calls

Tested by

no test coverage detected