Skip to content

Commit

Permalink
SetParameterType: Fix compatibility with TypeScript 5.4 (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiyaaaaa committed Mar 20, 2024
1 parent 14fb192 commit a186adb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -16,7 +16,8 @@
"node": ">=16"
},
"scripts": {
"test": "xo && tsd && tsc && node script/test/source-files-extension.js"
"test": "xo && tsd && tsc && npm run test:set-parameter-type && node script/test/source-files-extension.js",
"test:set-parameter-type": "tsc --noEmit test-d/set-parameter-type"
},
"files": [
"index.d.ts",
Expand Down
7 changes: 5 additions & 2 deletions source/set-parameter-type.d.ts
@@ -1,5 +1,5 @@
import type {IsUnknown} from './is-unknown';
import type {StaticPartOfArray} from './internal';
import type {StaticPartOfArray, VariablePartOfArray} from './internal';
import type {UnknownArray} from './unknown-array';

/**
Expand Down Expand Up @@ -32,7 +32,10 @@ type MergeObjectToArray<TArray extends UnknownArray, TObject, TArrayCopy extends
? number extends TObject['length']
? TObject
: {
[K in keyof TArray]: K extends keyof TObject ? TObject[K] : TArray[K]
[K in keyof TArray]:
number extends K
? VariablePartOfArray<TArray>[number]
: K extends keyof TObject ? TObject[K] : TArray[K]
}
: TObject extends object
// If `TObject` is a object witch key is number like `{0: string, 1: number}`
Expand Down
6 changes: 3 additions & 3 deletions test-d/set-parameter-type.ts
Expand Up @@ -22,9 +22,9 @@ expectType<(a: string, b: string, c: boolean, ...args: boolean[]) => null>(test2
test2('1', '2', true, true);

// Test another define way
declare const test3: SetParameterType<typeof fn, [a: string, b: boolean]>;
expectType<(a: string, b: boolean, c: Object, ...args: boolean[]) => null>(test3);
test3('1', true, {}, true);
declare const test3: SetParameterType<typeof fn, [a: 'a', b: 'b']>;
expectType<(a: 'a', b: 'b', c: Object, ...args: boolean[]) => null>(test3);
test3('a', 'b', {}, true);

// Test `...args` parameter
declare const testargs: SetParameterType<typeof fn, {3: string}>;
Expand Down

0 comments on commit a186adb

Please sign in to comment.