Skip to content

Commit

Permalink
fix: Join type (#1427)
Browse files Browse the repository at this point in the history
* fix: narrow T and narrow array item types

* fix: guarantee Join<R, D> is a string
  • Loading branch information
zewa666 committed Mar 8, 2024
1 parent d3d2d39 commit 21c76cc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
65 changes: 65 additions & 0 deletions packages/common/src/interfaces/__tests__/column.interface.spec.ts
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);
});
});
12 changes: 5 additions & 7 deletions packages/common/src/interfaces/column.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ import type {
SortComparer,
} from './index';

type PathsToStringProps<T> = T extends string | number | boolean | Date ? [] : {
export type PathsToStringProps<T> = T extends string | number | boolean | Date ? [] : {
[K in Extract<keyof T, string>]: [K, ...PathsToStringProps<T[K]>]
}[Extract<keyof T, string>];

/* eslint-disable @typescript-eslint/indent */
// disable eslint indent rule until this issue is fixed: https://github.com/typescript-eslint/typescript-eslint/issues/1824
type Join<T extends any[], D extends string> =
type AllowedJoinTypes = string | number | boolean;

export type Join<T extends (AllowedJoinTypes | unknown )[], D extends string> =
T extends [] ? never :
T extends [infer F] ? F :
T extends [infer F, ...infer R] ?
// @ts-ignore
F extends string ? string extends F ? string : `${F}${D}${Join<R, D>}` : never : string;
/* eslint-enable @typescript-eslint/indent */
F extends AllowedJoinTypes ? string extends F ? string : `${F}${D}${Join<Extract<R, AllowedJoinTypes[]>, D>}` : never : string;

export interface Column<T = any> {
/** Defaults to false, should we always render the column? */
Expand Down

0 comments on commit 21c76cc

Please sign in to comment.