Skip to content

Commit

Permalink
ReadonlyDeep/WritableDeep: Fix TypeScript 5.4 compatibility (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Apr 2, 2024
1 parent 0fb2d62 commit 2878773
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"devDependencies": {
"expect-type": "^0.15.0",
"npm-run-all2": "^6.1.2",
"tsd": "^0.29.0",
"tsd": "^0.31.0",
"typescript": "~5.4.3",
"xo": "^0.58.0"
},
Expand Down
8 changes: 5 additions & 3 deletions source/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,11 @@ Multiple call signatures cannot currently be supported due to a TypeScript limit
@see https://github.com/microsoft/TypeScript/issues/29732
*/
export type HasMultipleCallSignatures<T extends (...arguments_: any[]) => unknown> =
T extends {(...arguments_: infer A): unknown; (...arguments_: any[]): unknown}
? unknown[] extends A
? false
T extends {(...arguments_: infer A): unknown; (...arguments_: infer B): unknown}
? B extends A
? A extends B
? false
: true
: true
: false;

Expand Down
22 changes: 22 additions & 0 deletions test-d/internal/has-multiple-call-signatures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {expectType} from 'tsd';
import type {HasMultipleCallSignatures} from '../../source/internal';

type Overloaded = {
(foo: number): string;
(foo: string, bar: number): number;
};

type Overloaded2 = {
(foo: number | undefined): string;
// eslint-disable-next-line @typescript-eslint/unified-signatures
(foo: number): string;
};

type Namespace = {
(foo: number): string;
baz: boolean[];
};

expectType<true>({} as HasMultipleCallSignatures<Overloaded>);
expectType<true>({} as HasMultipleCallSignatures<Overloaded2>);
expectType<false>({} as HasMultipleCallSignatures<Namespace>);
15 changes: 15 additions & 0 deletions test-d/readonly-deep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,18 @@ type VoidTypeExpected = {
};
declare const voidType: ReadonlyDeep<VoidType>;
expectType<VoidTypeExpected>(voidType);

// Standalone tests

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const readonlyNamespace = {} as ReadonlyDeep<{
(foo: number): string;
baz: boolean[];
}>;
expectType<((foo: number) => string) & {
readonly baz: readonly boolean[];
}>(readonlyNamespace);
expectAssignable<{
(foo: number): string;
readonly baz: readonly boolean[];
}>(readonlyNamespace);
15 changes: 15 additions & 0 deletions test-d/writable-deep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,18 @@ const fullyWritableData = {
},
};
expectAssignable<WritableDeep<ReadonlyDeep<typeof fullyWritableData>>>(fullyWritableData);

// Standalone tests

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const writableNamespace = {} as WritableDeep<{
(foo: number): string;
readonly baz: readonly boolean[];
}>;
expectType<((foo: number) => string) & {
baz: boolean[];
}>(writableNamespace);
expectAssignable<{
(foo: number): string;
baz: boolean[];
}>(writableNamespace);
6 changes: 1 addition & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,5 @@
// "noUncheckedIndexedAccess": true, // TODO: Enable.
"noPropertyAccessFromIndexSignature": true,
"useDefineForClassFields": true,
},
"exclude": [
// Ignore `WriteableDeep` error temporarily, remove when #833 are fixed.
"test-d/writable-deep.ts",
]
}
}

0 comments on commit 2878773

Please sign in to comment.