|
| 1 | +/** checks to see if `value` is a T */ |
| 2 | +type check<T> = (value: unknown) => value is T; |
| 3 | + |
| 4 | +interface t { |
| 5 | + // lua types |
| 6 | + /** checks to see if `value` is an any */ |
| 7 | + any: (value: unknown) => value is any; |
| 8 | + /** checks to see if `value` is a boolean */ |
| 9 | + boolean: (value: unknown) => value is boolean; |
| 10 | + /** checks to see if `value` is a thread */ |
| 11 | + coroutine: (value: unknown) => value is thread; |
| 12 | + /** checks to see if `value` is a Function */ |
| 13 | + callback: (value: unknown) => value is Function; |
| 14 | + /** checks to see if `value` is undefined */ |
| 15 | + none: (value: unknown) => value is undefined; |
| 16 | + /** checks to see if `value` is a number, will _not_ match NaN */ |
| 17 | + number: (value: unknown) => value is number; |
| 18 | + /** checks to see if `value` is NaN */ |
| 19 | + nan: (value: unknown) => value is number; |
| 20 | + /** checks to see if `value` is a string */ |
| 21 | + string: (value: unknown) => value is string; |
| 22 | + /** checks to see if `value` is an object */ |
| 23 | + table: (value: unknown) => value is object; |
| 24 | + /** checks to see if `value` is a userdata */ |
| 25 | + userdata: (value: unknown) => value is object; |
| 26 | + |
| 27 | + // roblox types |
| 28 | + /** checks to see if `value` is an Axes */ |
| 29 | + Axes: (value: unknown) => value is Axes; |
| 30 | + /** checks to see if `value` is a BrickColor */ |
| 31 | + BrickColor: (value: unknown) => value is BrickColor; |
| 32 | + /** checks to see if `value` is a CFrame */ |
| 33 | + CFrame: (value: unknown) => value is CFrame; |
| 34 | + /** checks to see if `value` is a Color3 */ |
| 35 | + Color3: (value: unknown) => value is Color3; |
| 36 | + /** checks to see if `value` is a ColorSequence */ |
| 37 | + ColorSequence: (value: unknown) => value is ColorSequence; |
| 38 | + /** checks to see if `value` is a ColorSequenceKeypoint */ |
| 39 | + ColorSequenceKeypoint: (value: unknown) => value is ColorSequenceKeypoint; |
| 40 | + /** checks to see if `value` is a DockWidgetPluginGuiInfo */ |
| 41 | + DockWidgetPluginGuiInfo: (value: unknown) => value is DockWidgetPluginGuiInfo; |
| 42 | + /** checks to see if `value` is a Faces */ |
| 43 | + Faces: (value: unknown) => value is Faces; |
| 44 | + /** checks to see if `value` is an Instance */ |
| 45 | + Instance: (value: unknown) => value is Instance; |
| 46 | + /** checks to see if `value` is a NumberRange */ |
| 47 | + NumberRange: (value: unknown) => value is NumberRange; |
| 48 | + /** checks to see if `value` is a NumberSequence */ |
| 49 | + NumberSequence: (value: unknown) => value is NumberSequence; |
| 50 | + /** checks to see if `value` is a NumberSequenceKeypoint */ |
| 51 | + NumberSequenceKeypoint: (value: unknown) => value is NumberSequenceKeypoint; |
| 52 | + /** checks to see if `value` is a PathWaypoint */ |
| 53 | + PathWaypoint: (value: unknown) => value is PathWaypoint; |
| 54 | + /** checks to see if `value` is a PhysicalProperties */ |
| 55 | + PhysicalProperties: (value: unknown) => value is PhysicalProperties; |
| 56 | + /** checks to see if `value` is a Random */ |
| 57 | + Random: (value: unknown) => value is Random; |
| 58 | + /** checks to see if `value` is a Ray */ |
| 59 | + Ray: (value: unknown) => value is Ray; |
| 60 | + /** checks to see if `value` is a Rect */ |
| 61 | + Rect: (value: unknown) => value is Rect; |
| 62 | + /** checks to see if `value` is a Region3 */ |
| 63 | + Region3: (value: unknown) => value is Region3; |
| 64 | + /** checks to see if `value` is a Region3int16 */ |
| 65 | + Region3int16: (value: unknown) => value is Region3int16; |
| 66 | + /** checks to see if `value` is a TweenInfo */ |
| 67 | + TweenInfo: (value: unknown) => value is TweenInfo; |
| 68 | + /** checks to see if `value` is a UDim */ |
| 69 | + UDim: (value: unknown) => value is UDim; |
| 70 | + /** checks to see if `value` is a UDim2 */ |
| 71 | + UDim2: (value: unknown) => value is UDim2; |
| 72 | + /** checks to see if `value` is a Vector2 */ |
| 73 | + Vector2: (value: unknown) => value is Vector2; |
| 74 | + /** checks to see if `value` is a Vector3 */ |
| 75 | + Vector3: (value: unknown) => value is Vector3; |
| 76 | + /** checks to see if `value` is a Vector3int16 */ |
| 77 | + Vector3int16: (value: unknown) => value is Vector3int16; |
| 78 | + |
| 79 | + /** |
| 80 | + * checks to see if `value == literalValue`\ |
| 81 | + * If your `literalValue` is not a primitive, use t.exactly instead. |
| 82 | + */ |
| 83 | + literal: <T extends string | number | boolean | undefined>(literalValue: T) => (value: unknown) => value is T; |
| 84 | + /** checks to see if `value == literalValue` */ |
| 85 | + exactly: <T>(literalValue: T) => (value: unknown) => value is T; |
| 86 | + |
| 87 | + /** checks to see if `value` is an integer */ |
| 88 | + integer: (value: unknown) => value is number; |
| 89 | + /** checks to see if `value` is a number and is more than or equal to `min` */ |
| 90 | + numberMin: (min: number) => (value: unknown) => value is number; |
| 91 | + /** checks to see if `value` is a number and is less than or equal to `max` */ |
| 92 | + numberMax: (max: number) => (value: unknown) => value is number; |
| 93 | + /** checks to see if `value` is a number and is more than `min` */ |
| 94 | + numberMinExclusive: (min: number) => (value: unknown) => value is number; |
| 95 | + /** checks to see if `value` is a number and is less than `max` */ |
| 96 | + numberMaxExclusive: (max: number) => (value: unknown) => value is number; |
| 97 | + /** checks to see if `value` is a number and is more than 0 */ |
| 98 | + numberPositive: (value: unknown) => value is number; |
| 99 | + /** checks to see if `value` is a number and is less than 0 */ |
| 100 | + numberNegative: (value: unknown) => value is number; |
| 101 | + /** checks to see if `value` is a number and `min <= value <= max` */ |
| 102 | + numberConstrained: (min: number, max: number) => (value: unknown) => value is number; |
| 103 | + /** checks to see if `value` is a number and `min < value < max` */ |
| 104 | + numberConstrainedExclusive: (min: number, max: number) => (value: unknown) => value is number; |
| 105 | + /** checks to see if `value` is either nil or passes `check` */ |
| 106 | + optional: <T>(check: (value: unknown) => value is T) => check<T | undefined>; |
| 107 | + /** checks to see if `value` is a table and if its keys match against `check */ |
| 108 | + keys: <T>(check: (value: unknown) => value is T) => check<Map<T, unknown>>; |
| 109 | + /** checks to see if `value` is a table and if its values match against `check` */ |
| 110 | + values: <T>(check: (value: unknown) => value is T) => check<Map<unknown, T>>; |
| 111 | + /** checks to see if `value` is a table and all of its keys match against `keyCheck` and all of its values match against `valueCheck` */ |
| 112 | + map: <K, V>( |
| 113 | + keyCheck: (value: unknown) => value is K, |
| 114 | + valueCheck: (value: unknown) => value is V |
| 115 | + ) => check<Map<K, V>>; |
| 116 | + /** checks to see if `value` is an array and all of its keys are sequential integers and all of its values match `check` */ |
| 117 | + array: <T>(check: (value: unknown) => value is T) => check<Array<T>>; |
| 118 | + |
| 119 | + /** checks to see if `value` matches any given check */ |
| 120 | + union: <T extends Array<any>>( |
| 121 | + ...args: T |
| 122 | + ) => T extends [check<infer A>] |
| 123 | + ? (value: unknown) => value is A |
| 124 | + : T extends [check<infer A>, check<infer B>] |
| 125 | + ? check<A | B> |
| 126 | + : T extends [check<infer A>, check<infer B>, check<infer C>] |
| 127 | + ? check<A | B | C> |
| 128 | + : T extends [check<infer A>, check<infer B>, check<infer C>, check<infer D>] |
| 129 | + ? check<A | B | C | D> |
| 130 | + : T extends [check<infer A>, check<infer B>, check<infer C>, check<infer D>, check<infer E>] |
| 131 | + ? check<A | B | C | D | E> |
| 132 | + : T extends [check<infer A>, check<infer B>, check<infer C>, check<infer D>, check<infer E>, check<infer F>] |
| 133 | + ? check<A | B | C | D | E | F> |
| 134 | + : never; |
| 135 | + |
| 136 | + /** checks to see if `value` matches all given checks */ |
| 137 | + intersection: <T extends Array<any>>( |
| 138 | + ...args: T |
| 139 | + ) => T extends [check<infer A>] |
| 140 | + ? (value: unknown) => value is A |
| 141 | + : T extends [check<infer A>, check<infer B>] |
| 142 | + ? check<A & B> |
| 143 | + : T extends [check<infer A>, check<infer B>, check<infer C>] |
| 144 | + ? check<A & B & C> |
| 145 | + : T extends [check<infer A>, check<infer B>, check<infer C>, check<infer D>] |
| 146 | + ? check<A & B & C & D> |
| 147 | + : T extends [check<infer A>, check<infer B>, check<infer C>, check<infer D>, check<infer E>] |
| 148 | + ? check<A & B & C & D & E> |
| 149 | + : T extends [check<infer A>, check<infer B>, check<infer C>, check<infer D>, check<infer E>, check<infer F>] |
| 150 | + ? check<A & B & C & D & E & F> |
| 151 | + : never; |
| 152 | + |
| 153 | + /** checks to see if `value` matches a given interface definition */ |
| 154 | + interface: <T extends { [index: string]: (value: unknown) => value is any }>( |
| 155 | + checkTable: T |
| 156 | + ) => check<{ [P in keyof T]: t.static<T[P]> }>; |
| 157 | + |
| 158 | + /** checks to see if `value` matches a given interface definition with no extra members */ |
| 159 | + strictInterface: <T extends { [index: string]: (value: unknown) => value is any }>( |
| 160 | + checkTable: T |
| 161 | + ) => check<{ [P in keyof T]: t.static<T[P]> }>; |
| 162 | +} |
| 163 | + |
| 164 | +interface t { |
| 165 | + instance: <T extends string>(className: T) => T extends keyof Instances ? check<Instances[T]> : boolean; |
| 166 | + instanceIsA: <T extends string>(className: T) => T extends keyof Instances ? check<Instances[T]> : boolean; |
| 167 | +} |
| 168 | + |
| 169 | +declare namespace t { |
| 170 | + /** creates a static type from a t-defined type */ |
| 171 | + export type static<T> = T extends check<infer U> ? U : never; |
| 172 | +} |
| 173 | + |
| 174 | +declare const t: t; |
| 175 | +export = t; |
0 commit comments