Skip to content

Commit

Permalink
Fix TypeScript error on 'next' (jimp-dev#858)
Browse files Browse the repository at this point in the history
* Fix TypeScript error on 'next'

* Further work to fix type tests
  • Loading branch information
Corbin Crutchley authored Mar 18, 2020
1 parent ecadad6 commit 8eb2e9b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
15 changes: 10 additions & 5 deletions packages/core/types/jimp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,19 @@ export interface Jimp extends JimpConstructors {
): this;

// Functions
appendConstructorOption<T extends any[]>(
/**
* 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<Args extends any[], J extends Jimp = this>(
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<this>): Promise<this>;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export type UnionToIntersection<U> =
* Left loose as "any" in order to enable the GetPluginValue to work properly
*/
export type WellFormedValues<T extends any> =
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
Expand Down
18 changes: 18 additions & 0 deletions packages/custom/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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();
}
);
});
13 changes: 9 additions & 4 deletions packages/jimp/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,19 @@ interface DepreciatedJimp {
mask(src: this, x: number, y: number, cb?: ImageCallback): this;

// Functions
appendConstructorOption<T extends any[]>(
/**
* 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<Args extends any[]>(
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<this>;
Expand Down
13 changes: 13 additions & 0 deletions packages/jimp/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
);
});

0 comments on commit 8eb2e9b

Please sign in to comment.