Skip to content

Commit

Permalink
feat(types): AbstractConstructor
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 12, 2023
1 parent 0c1f420 commit f913557
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/types/__tests__/constructor-abstract.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @file Type Tests - AbstractConstructor
* @module tutils/types/tests/unit-d/AbstractConstructor
*/

import type TestSubject from '../constructor-abstract'

describe('unit-d:types/AbstractConstructor', () => {
abstract class Vehicle {
constructor(
public readonly vin: number,
public readonly make: string,
public readonly model: string,
public readonly year: number
) {}
}

it('should match abstract class declaration', () => {
assertType<TestSubject<Vehicle, [number, string, string, number]>>(Vehicle)
})
})
7 changes: 5 additions & 2 deletions src/types/__tests__/constructor.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
* @module tutils/types/tests/unit-d/Constructor
*/

import Person from '#fixtures/person'
import type TestSubject from '../constructor'

describe('unit-d:types/Constructor', () => {
class Dog {
constructor(public name: string) {}
}

it('should match class declaration', () => {
assertType<TestSubject<Person, [string, string]>>(Person)
assertType<TestSubject<Dog, [string, string]>>(Dog)
})
})
18 changes: 18 additions & 0 deletions src/types/constructor-abstract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file Type Definitions - AbstractConstructor
* @module tutils/types/AbstractConstructor
*/

/**
* An [`abstract class` constructor][1].
*
* [1]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-2.html#abstract-construct-signatures
*
* @template T - Class prototype
* @template A - Constructor arguments
*/
type AbstractConstructor<T, A extends unknown[] = any[]> = abstract new (
...args: A
) => T

export type { AbstractConstructor as default }
4 changes: 2 additions & 2 deletions src/types/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*
* [1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Classes/constructor
*
* @template P - Class prototype
* @template T - Class prototype
* @template A - Constructor arguments
*/
type Constructor<P, A extends unknown[] = any[]> = new (...args: A) => P
type Constructor<T, A extends unknown[] = any[]> = new (...args: A) => T

export type { Constructor as default }
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type { default as BuiltIn } from './built-in'
export type { default as Class } from './class'
export type { default as ComparisonOperator } from './comparison-operator'
export type { default as Constructor } from './constructor'
export type { default as AbstractConstructor } from './constructor-abstract'
export type { default as ContinuousValue } from './continuous-value'
export type { default as EmptyString } from './empty-string'
export type { default as EmptyValue } from './empty-value'
Expand Down

0 comments on commit f913557

Please sign in to comment.