Skip to content

Commit

Permalink
feat: ✨ Add the Extends type - Check if one type extends from another
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBroughton committed Apr 16, 2024
1 parent 99a88c6 commit 5566ad6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-feet-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@crbroughton/ts-test-utils": minor
---

Add the Extends type - Check if one type extends from another
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export type Includes<T extends U, U> = T extends U ? true : false
export type Excludes<T, U> = [T] extends [U] ? false : true

export type Assignable<T, U extends T> = U extends T ? true : false

export type Extends<T, U> = U extends T ? true : false
20 changes: 20 additions & 0 deletions tests/Extends.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Expect, Extends } from '..'

describe('Extends tests', () => {
it('Passes the extend test when the resulting type extends another type', () => {
interface BaseType { id: number }
type ExtensionType = BaseType & { name: string }
type Result = Expect<Extends<BaseType, ExtensionType>>
// ^?
})
it('Failed the includes test when the resulting type does not include the sub-type', () => {
// @ts-expect-error - Object / Record failing the equality checker
type Result = Expect<Includes<{ id: number, name: string }, { hobby: string }>>
// ^?
})
})



0 comments on commit 5566ad6

Please sign in to comment.