Skip to content

Commit

Permalink
Handle types that implicitly relied on @types/node
Browse files Browse the repository at this point in the history
Follow up to #898
  • Loading branch information
voxpelli committed Jul 11, 2024
1 parent d119c0f commit d256c8e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
17 changes: 15 additions & 2 deletions source/structured-cloneable.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type {IfAny} from './if-any';
import type {TypedArray} from './typed-array';

// Include if the type resolves to an actual type, else do not include it. Enables progressive enhancement
type IncludeIfNotAny<T> = IfAny<T, never, T>;

type StructuredCloneablePrimitive =
| string
| number
Expand All @@ -18,8 +22,17 @@ type StructuredCloneableData =
| Error
| RegExp
| TypedArray
| Blob
| File;
| IncludeIfNotAny<
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore Requires dom or @types/node
Blob
>
// eslint-disable-next-line @typescript-eslint/no-duplicate-type-constituents
| IncludeIfNotAny<
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore Requires dom or @types/node@>=20
File
>;
// DOM exclusive types
// | AudioData
// | CropTarget
Expand Down
5 changes: 2 additions & 3 deletions test-d/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import {expectAssignable, expectNotAssignable, expectType} from 'tsd';
import type {Class, Constructor, IsAny} from '../index';

class Foo {
constructor(x: number, y: any) {
console.log(x, y);
}
// eslint-disable-next-line @typescript-eslint/no-useless-constructor, @typescript-eslint/no-empty-function
constructor(_x: number, _y: any) {}

// eslint-disable-next-line @typescript-eslint/no-empty-function
method(): void {}
Expand Down
2 changes: 0 additions & 2 deletions test-d/multidimensional-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ function createArray<T extends number>(dimensions: T): MultidimensionalArray<unk

let array = root;
for (let dimension = 1; dimension < dimensions; ++dimension) {
console.log(`Initializing dimension #${dimension}`);

array[0] = [];
array = array[0] as unknown[];
}
Expand Down
2 changes: 0 additions & 2 deletions test-d/multidimensional-readonly-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ function createArray<T extends number>(dimensions: T): MultidimensionalReadonlyA

let array = root;
for (let dimension = 1; dimension < dimensions; ++dimension) {
console.log(`Initializing dimension #${dimension}`);

array[0] = [];
if (dimension < dimensions - 1) {
array = array[0] as unknown[];
Expand Down
6 changes: 6 additions & 0 deletions test-d/set-parameter-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ function function_(_a: number, _b: string, _c: Object, ..._arguments: boolean[])
return null;
}

// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore Global requires @types/node
function functionWithThis(this: Global, _a: number) {
return null;
}
Expand Down Expand Up @@ -41,5 +43,9 @@ expectType<(a: string) => null>(test5);

// Test the function that has `this` parameter
declare const testThis: SetParameterType<typeof functionWithThis, {0: string}>;
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore Global requires @types/node
expectType<(this: Global, a: string) => null>(testThis);
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore global requires @types/node
testThis.call(global, '1');
4 changes: 4 additions & 0 deletions test-d/structured-cloneable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ expectAssignable<StructuredCloneable>(new Set<string[]>());
expectAssignable<StructuredCloneable>(new Set<Set<string>>());

// Web/API types
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore Requires dom or @types/node@>=20
declare const blob: Blob;
expectAssignable<StructuredCloneable>(blob);
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore Requires dom or @types/node@>=20
declare const file: File;
expectAssignable<StructuredCloneable>(file);

0 comments on commit d256c8e

Please sign in to comment.