From 8eb2e9b8dbb2f1cf7c4a4d0da5d2a3436eb83543 Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Wed, 18 Mar 2020 09:50:14 -0700 Subject: [PATCH] Fix TypeScript error on 'next' (#858) * Fix TypeScript error on 'next' * Further work to fix type tests --- packages/core/types/jimp.d.ts | 15 ++++++++++----- packages/core/types/utils.d.ts | 4 ++-- packages/custom/types/test.ts | 18 ++++++++++++++++++ packages/jimp/types/index.d.ts | 13 +++++++++---- packages/jimp/types/test.ts | 13 +++++++++++++ 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/packages/core/types/jimp.d.ts b/packages/core/types/jimp.d.ts index b496adce2..97fdce791 100644 --- a/packages/core/types/jimp.d.ts +++ b/packages/core/types/jimp.d.ts @@ -170,14 +170,19 @@ export interface Jimp extends JimpConstructors { ): this; // Functions - appendConstructorOption( + /** + * I'd like to make `Args` generic and used in `run` and `test` but alas, + * it's not possible RN: + * https://github.com/microsoft/TypeScript/issues/26113 + */ + appendConstructorOption( name: string, - test: (...args: T[]) => boolean, + test: (...args: any[]) => boolean, run: ( - this: this, - resolve: (jimp: this) => any, + this: J, + resolve: (jimp?: J) => any, reject: (reason: Error) => any, - ...args: T[] + ...args: any[] ) => any ): void; read(path: string, cb?: ImageCallback): Promise; diff --git a/packages/core/types/utils.d.ts b/packages/core/types/utils.d.ts index 7a733d79c..72d63a555 100644 --- a/packages/core/types/utils.d.ts +++ b/packages/core/types/utils.d.ts @@ -13,8 +13,8 @@ export type UnionToIntersection = * Left loose as "any" in order to enable the GetPluginValue to work properly */ export type WellFormedValues = - T['class'] & - T['constants']; + (T extends {class: any} ? T['class'] : {}) & + (T extends {constants: any} ? T['constants'] : {}); // Util type for the functions that deal with `@jimp/custom` // Must accept any or no props thanks to typing of the `plugins` intersected function diff --git a/packages/custom/types/test.ts b/packages/custom/types/test.ts index 82f83fe3f..8b4ce6d65 100644 --- a/packages/custom/types/test.ts +++ b/packages/custom/types/test.ts @@ -6,6 +6,7 @@ import resize from '@jimp/plugin-resize'; import scale from '@jimp/plugin-scale'; import types from '@jimp/types'; import plugins from '@jimp/plugins'; +import * as Jimp from 'jimp'; // configure should return a valid Jimp type with addons const CustomJimp = configure({ @@ -350,3 +351,20 @@ test('can handle only one plugin', () => { // $ExpectError Jiimp.func(); }); + + +test('Can handle appendConstructorOption', () => { + const AppendJimp = configure({}); + + AppendJimp.appendConstructorOption( + 'Name of Option', + args => args.hasSomeCustomThing, + function(resolve, reject, args) { + // $ExpectError + this.bitmap = 3; + // $ExpectError + AppendJimp.resize(2, 2); + resolve(); + } + ); +}); diff --git a/packages/jimp/types/index.d.ts b/packages/jimp/types/index.d.ts index d8881787f..88687bdf4 100644 --- a/packages/jimp/types/index.d.ts +++ b/packages/jimp/types/index.d.ts @@ -341,14 +341,19 @@ interface DepreciatedJimp { mask(src: this, x: number, y: number, cb?: ImageCallback): this; // Functions - appendConstructorOption( + /** + * I'd like to make `Args` generic and used in `run` and `test` but alas, + * it's not possible RN: + * https://github.com/microsoft/TypeScript/issues/26113 + */ + appendConstructorOption( name: string, - test: (...args: T[]) => boolean, + test: (...args: any[]) => boolean, run: ( this: this, - resolve: (jimp: this) => any, + resolve: (jimp?: this) => any, reject: (reason: Error) => any, - ...args: T[] + ...args: any[] ) => any ): void; read(path: string, cb?: ImageCallback): Promise; diff --git a/packages/jimp/types/test.ts b/packages/jimp/types/test.ts index 13a257150..863484047 100644 --- a/packages/jimp/types/test.ts +++ b/packages/jimp/types/test.ts @@ -86,3 +86,16 @@ test('Can handle callback with constructor', () => { cbJimpInst.func(); }); }) + +test('Can handle appendConstructorOption', () => { + Jimp.appendConstructorOption( + 'Name of Option', + args => args.hasSomeCustomThing, + function(resolve, reject, args) { + // $ExpectError + this.bitmap = 3; + Jimp.resize(2, 2); + resolve(); + } + ); +});