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

Update xo #838

Merged
merged 3 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
10 changes: 8 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
name: CI
on:
- push
- pull_request
push:
branches:
- main
tags:
- '*'
pull_request:
branches:
- main
jobs:
test:
name: Node.js ${{ matrix.node-version }}
Expand Down
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
"node": ">=16"
},
"scripts": {
"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"
"test:set-parameter-type": "tsc --noEmit test-d/set-parameter-type",
"test:source-files-extension": "node script/test/source-files-extension.js",
"test:tsc": "tsc",
"test:tsd": "tsd",
"test:xo": "xo",
"test": "run-p test:*"
},
"files": [
"index.d.ts",
Expand All @@ -38,9 +42,10 @@
"devDependencies": {
"@sindresorhus/tsconfig": "~0.7.0",
"expect-type": "^0.15.0",
"npm-run-all2": "^6.1.2",
"tsd": "^0.28.1",
"typescript": "^5.2.2",
"xo": "^0.56.0"
"xo": "^0.58.0"
},
"xo": {
"rules": {
Expand All @@ -50,7 +55,11 @@
"import/extensions": "off",
"@typescript-eslint/no-redeclare": "off",
"@typescript-eslint/no-confusing-void-expression": "off",
"@typescript-eslint/no-unsafe-argument": "off"
"@typescript-eslint/no-unsafe-argument": "off",
"object-curly-newline": ["error", {
"multiline": true,
"consistent": true
}]
}
},
"tsd": {
Expand Down
2 changes: 1 addition & 1 deletion source/asyncify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ const getFooAsync: AsyncifiedFooGetter = (someArg) => {

@category Async
*/
export type Asyncify<Fn extends (...arguments_: any[]) => any> = SetReturnType<Fn, Promise<Awaited<ReturnType<Fn>>>>;
export type Asyncify<Function_ extends (...arguments_: any[]) => any> = SetReturnType<Function_, Promise<Awaited<ReturnType<Function_>>>>;
2 changes: 1 addition & 1 deletion source/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export type BuiltIns = Primitive | void | Date | RegExp;
/**
Matches non-recursive types.
*/
export type NonRecursiveType = BuiltIns | Function | (new (...args: any[]) => unknown);
export type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown);

/**
Returns a boolean for whether the given type is a plain key-value object.
Expand Down
2 changes: 1 addition & 1 deletion source/readonly-deep.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Note that types containing overloaded functions are not made deeply readonly due
*/
export type ReadonlyDeep<T> = T extends BuiltIns
? T
: T extends new (...args: any[]) => unknown
: T extends new (...arguments_: any[]) => unknown
? T // Skip class constructors
: T extends (...arguments_: any[]) => unknown
? {} extends ReadonlyObjectDeep<T>
Expand Down
12 changes: 6 additions & 6 deletions source/set-parameter-type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ type HandleLog2 = SetParameterType<HandleMessage, {2: string}>;

@category Function
*/
export type SetParameterType<Fn extends (...arguments_: any[]) => unknown, P extends Record<number, unknown>> =
export type SetParameterType<Function_ extends (...arguments_: any[]) => unknown, P extends Record<number, unknown>> =
// Just using `Parameters<Fn>` isn't ideal because it doesn't handle the `this` fake parameter.
Fn extends (this: infer ThisArg, ...arguments_: infer Arguments) => unknown
Function_ extends (this: infer ThisArgument, ...arguments_: infer Arguments) => unknown
? (
// If a function did not specify the `this` fake parameter, it will be inferred to `unknown`.
// We want to detect this situation just to display a friendlier type upon hovering on an IntelliSense-powered IDE.
IsUnknown<ThisArg> extends true
? (...arguments_: MergeObjectToArray<Arguments, P>) => ReturnType<Fn>
: (this: ThisArg, ...arguments_: MergeObjectToArray<Arguments, P>) => ReturnType<Fn>
IsUnknown<ThisArgument> extends true
? (...arguments_: MergeObjectToArray<Arguments, P>) => ReturnType<Function_>
: (this: ThisArgument, ...arguments_: MergeObjectToArray<Arguments, P>) => ReturnType<Function_>
)
: Fn; // This part should be unreachable
: Function_; // This part should be unreachable
8 changes: 4 additions & 4 deletions source/set-return-type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ type MyWrappedFunction = SetReturnType<MyFunctionThatCanThrow, SomeOtherType | u

@category Function
*/
export type SetReturnType<Fn extends (...arguments_: any[]) => any, TypeToReturn> =
export type SetReturnType<Function_ extends (...arguments_: any[]) => any, TypeToReturn> =
// Just using `Parameters<Fn>` isn't ideal because it doesn't handle the `this` fake parameter.
Fn extends (this: infer ThisArg, ...arguments_: infer Arguments) => any ? (
Function_ extends (this: infer ThisArgument, ...arguments_: infer Arguments) => any ? (
// If a function did not specify the `this` fake parameter, it will be inferred to `unknown`.
// We want to detect this situation just to display a friendlier type upon hovering on an IntelliSense-powered IDE.
IsUnknown<ThisArg> extends true ? (...arguments_: Arguments) => TypeToReturn : (this: ThisArg, ...arguments_: Arguments) => TypeToReturn
IsUnknown<ThisArgument> extends true ? (...arguments_: Arguments) => TypeToReturn : (this: ThisArgument, ...arguments_: Arguments) => TypeToReturn
) : (
// This part should be unreachable, but we make it meaningful just in case…
(...arguments_: Parameters<Fn>) => TypeToReturn
(...arguments_: Parameters<Function_>) => TypeToReturn
);
1 change: 1 addition & 0 deletions source/tsconfig-json.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ declare namespace TsConfigJson {
| 'es2022'
| 'esnext';

// eslint-disable-next-line unicorn/prevent-abbreviations
export type Lib =
| 'ES5'
| 'ES6'
Expand Down
24 changes: 12 additions & 12 deletions test-d/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ class Foo {
method(): void {}
}

function fn(Cls: Constructor<Foo>): Foo {
function function_(Cls: Constructor<Foo>): Foo {
return new Cls(1, '', 123);
}

function fn2(Cls: Constructor<Foo, [number, number]>): Foo {
function function2(Cls: Constructor<Foo, [number, number]>): Foo {
expectError(new Cls(1, ''));
return new Cls(1, 2);
}

fn(Foo);
fn2(Foo);
function_(Foo);
function2(Foo);

// Prototype test
type PositionProps = {
type PositionProperties = {
top: number;
left: number;
};
Expand All @@ -39,21 +39,21 @@ class Position {
}
}

declare const Bar: Class<PositionProps>;
declare const Bar: Class<PositionProperties>;

expectAssignable<Class<PositionProps>>(Position);
expectAssignable<Class<PositionProperties>>(Position);

expectNotAssignable<Class<PositionProps, [number]>>(Position);
expectNotAssignable<Class<PositionProperties, [number]>>(Position);

expectAssignable<Class<PositionProps, [number, number]>>(Position);
expectAssignable<Constructor<PositionProps, [number, number]>>(Position);
expectAssignable<Class<PositionProperties, [number, number]>>(Position);
expectAssignable<Constructor<PositionProperties, [number, number]>>(Position);

expectType<IsAny<typeof Bar['prototype']>>(false);
expectType<PositionProps>(Position.prototype);
expectType<PositionProperties>(Position.prototype);
// /Prototype test

expectError(new Position(17));
expectAssignable<PositionProps>(new Position(17, 34));
expectAssignable<PositionProperties>(new Position(17, 34));

// Prototype test with type parameter
class Building<T = unknown> {
Expand Down
6 changes: 3 additions & 3 deletions test-d/conditional-pick-deep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Example = {
interface: InterfaceA;
instanceA: ClassA;
ClassA: typeof ClassA;
function: (...args: string[]) => string;
function: (...arguments_: string[]) => string;
stringOrBoolean: string | boolean;
object: {
string: string;
Expand Down Expand Up @@ -126,8 +126,8 @@ expectType<{instanceA: ClassA}>(instancePick);
declare const classPick: ConditionalPickDeep<Example, typeof ClassA>;
expectType<{ClassA: typeof ClassA}>(classPick);

declare const functionPick: ConditionalPickDeep<Example, (...args: string[]) => string>;
expectType<{function: (...args: string[]) => string}>(functionPick);
declare const functionPick: ConditionalPickDeep<Example, (...arguments_: string[]) => string>;
expectType<{function: (...arguments_: string[]) => string}>(functionPick);

declare const mapPick: ConditionalPickDeep<Example, Map<string, string>>;
expectType<{map: Map<string, string>}>(mapPick);
Expand Down