Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetParameterType: Fix compatibility with TypeScript 5.4 #836

Merged
merged 2 commits into from Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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