-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: narrow T and narrow array item types * fix: guarantee Join<R, D> is a string
- Loading branch information
Showing
2 changed files
with
70 additions
and
7 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
packages/common/src/interfaces/__tests__/column.interface.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { Join, PathsToStringProps } from '../column.interface'; | ||
|
||
type Expect<T extends true> = T; | ||
type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends < | ||
T | ||
>() => T extends Y ? 1 : 2 | ||
? true | ||
: false; | ||
|
||
const typeCheckValue = <T>(arg: T) => arg; | ||
|
||
describe('the Join type', () => { | ||
it('should concatenate strings with a separator', () => { | ||
const sut = 'a.b.c'; | ||
type TypeToTest = Join<['a', 'b', 'c'], '.'>; | ||
const value = typeCheckValue<TypeToTest>(sut); | ||
|
||
type Check = Equal<typeof value, typeof sut>; | ||
type Result = Expect<Check>; | ||
|
||
const x: Result = true; | ||
expect(x).toBe(true); | ||
}); | ||
|
||
it('should concatenate mixed types with a separator', () => { | ||
const sut = 'hello.10.true'; | ||
type TypeToTest = Join<["hello", 10, true], '.'>; | ||
const value = typeCheckValue<TypeToTest>(sut); | ||
|
||
type Check = Equal<typeof value, typeof sut>; | ||
type Result = Expect<Check>; | ||
|
||
const x: Result = true; | ||
expect(x).toBe(true); | ||
}); | ||
|
||
it('should construct a string path from flat strings', () => { | ||
const sut = 'a'; | ||
type TypeToTest = Join<PathsToStringProps<{ a: string; b: number; c: boolean }>, '.'>; | ||
const value = typeCheckValue<TypeToTest>(sut); | ||
|
||
type Check = Equal<typeof value, TypeToTest>; | ||
type Result = Expect<Check>; | ||
|
||
const x: Result = true; | ||
expect(x).toBe(true); | ||
}); | ||
|
||
it('should construct a string path from nested string properties', () => { | ||
const sut_failed = 'top'; | ||
const sut_success = 'top.nested'; | ||
type TypeToTest = Join<PathsToStringProps<{ top: { nested: string }, foo: string }>, '.'>; | ||
|
||
// can't assign top level property if its an object | ||
// @ts-expect-error | ||
typeCheckValue<TypeToTest>(sut_failed); | ||
const value = typeCheckValue<TypeToTest>(sut_success); | ||
|
||
type Check = Equal<typeof value, TypeToTest>; | ||
type Result = Expect<Check>; | ||
|
||
const x: Result = true; | ||
expect(x).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters