diff --git a/README.md b/README.md index 9aa87efb..0155994f 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ npm i ts-roids ``` -### Example +### Examples +#### Lock a class (make it immutable) and finalize it (prohibit further extension) ```ts import { locked, final } from 'ts-roids'; import type { @@ -24,6 +25,12 @@ export class Foo { } } ``` +#### Quickly validates types using IntelliSense +```typescript +type ResultType = TestType; +``` +``TestType`` accepts three arguments: the types you're comparing (``Type1`` and ``Type2``) and a boolean (true if you think they match, false otherwise). The resulting ``ResultType`` will tell if the choice is correct, true if the types actually match, else false. + ### Docs Checkout the inline documentation in `/src` along with `/tests` to see how it works. ### License diff --git a/src/index.ts b/src/index.ts index ad45445d..575ab479 100644 --- a/src/index.ts +++ b/src/index.ts @@ -147,6 +147,51 @@ export type DeepMutate = { -readonly [K in Keys]: DeepMutate; }; +type X = { + readonly a: () => 1; + readonly b: string; + readonly c: { + readonly d: boolean; + readonly e: { + readonly g: { + readonly h: { + readonly i: true; + readonly j: 's'; + }; + readonly k: 'hello'; + }; + }; + }; +}; + +type Expected = { + a: () => 1; + b: string; + c: { + d: boolean; + e: { + g: { + h: { + i: true; + j: 's'; + }; + k: 'hello'; + }; + }; + }; +}; + +export type DeepMutable, any>> = T extends Callable< + OneOrMany, + any +> + ? T + : { + -readonly [K in Keys]: DeepMutable; + }; + +export type ResultType = TestType, Expected, true>; + export type Immutate = { +readonly [K in Keys]: T[K]; }; @@ -203,6 +248,42 @@ export type DeepNonNullableKeys = { export type Callable = (...args: A) => R; +export type IfSame = T extends P ? Yes : No; +export type ShallowEquals = X extends Y ? true : false; +export type Equals = (() => T extends X ? true : false) extends < + T, +>() => T extends Y ? true : false + ? true + : false; + +export type MutableKeys = keyof { + [P in Keys as Equals, Readonly>> extends true + ? never + : P]: never; +}; + +export type ImmutableKeys = keyof { + [P in Keys as Equals, Readonly>> extends true + ? P + : never]: never; +}; + +declare function _testType(): Equals< + Equals, + E +>; +export type TestType = ReturnType< + typeof _testType +>; +// TODO: clean this shit +type Type1 = string; +type Type2 = string; +type Type3 = number; + +export type Result1 = TestType; +export type Result2 = TestType; + +/* class is fucking lockedin ong! */ export function locked(constructor: Newable): void { function _sealAndFreeze(obj: object): void { Object.seal(obj); @@ -223,17 +304,3 @@ export function final(target: T): T { } }; } - -export type IfSame = T extends P ? Yes : No; -export type ShallowEquals = X extends Y ? true : false; -export type Equals = (() => T extends X ? true : false) extends < - T, ->() => T extends Y ? true : false - ? true - : false; - -export type MutableKeys = keyof { - [P in Keys as Equals, Readonly>> extends true - ? never - : P]: never; -}; diff --git a/tests/test-type.test.ts b/tests/test-type.test.ts new file mode 100644 index 00000000..65334ef4 --- /dev/null +++ b/tests/test-type.test.ts @@ -0,0 +1,23 @@ +import { Keys, Primitive, Nullable, Numeric, TestType } from 'src'; +import { test, expect } from 'vitest'; + +type Foo = { + bar: string; + foo: number; + fooBar: string; +}; + +test('_', () => { + type PersonKeys = Keys; + const result: TestType = true; + expect(result).toBe(true); +}); + +test('_', () => { + const result: TestType< + Primitive, + symbol | Nullable | string | Numeric | boolean, + true + > = true; + expect(result).toBe(true); +});