Skip to content

Commit 8eb2e9b

Browse files
author
Corbin Crutchley
authored
Fix TypeScript error on 'next' (#858)
* Fix TypeScript error on 'next' * Further work to fix type tests
1 parent ecadad6 commit 8eb2e9b

File tree

5 files changed

+52
-11
lines changed

5 files changed

+52
-11
lines changed

packages/core/types/jimp.d.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,19 @@ export interface Jimp extends JimpConstructors {
170170
): this;
171171

172172
// Functions
173-
appendConstructorOption<T extends any[]>(
173+
/**
174+
* I'd like to make `Args` generic and used in `run` and `test` but alas,
175+
* it's not possible RN:
176+
* https://github.com/microsoft/TypeScript/issues/26113
177+
*/
178+
appendConstructorOption<Args extends any[], J extends Jimp = this>(
174179
name: string,
175-
test: (...args: T[]) => boolean,
180+
test: (...args: any[]) => boolean,
176181
run: (
177-
this: this,
178-
resolve: (jimp: this) => any,
182+
this: J,
183+
resolve: (jimp?: J) => any,
179184
reject: (reason: Error) => any,
180-
...args: T[]
185+
...args: any[]
181186
) => any
182187
): void;
183188
read(path: string, cb?: ImageCallback<this>): Promise<this>;

packages/core/types/utils.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export type UnionToIntersection<U> =
1313
* Left loose as "any" in order to enable the GetPluginValue to work properly
1414
*/
1515
export type WellFormedValues<T extends any> =
16-
T['class'] &
17-
T['constants'];
16+
(T extends {class: any} ? T['class'] : {}) &
17+
(T extends {constants: any} ? T['constants'] : {});
1818

1919
// Util type for the functions that deal with `@jimp/custom`
2020
// Must accept any or no props thanks to typing of the `plugins` intersected function

packages/custom/types/test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import resize from '@jimp/plugin-resize';
66
import scale from '@jimp/plugin-scale';
77
import types from '@jimp/types';
88
import plugins from '@jimp/plugins';
9+
import * as Jimp from 'jimp';
910

1011
// configure should return a valid Jimp type with addons
1112
const CustomJimp = configure({
@@ -350,3 +351,20 @@ test('can handle only one plugin', () => {
350351
// $ExpectError
351352
Jiimp.func();
352353
});
354+
355+
356+
test('Can handle appendConstructorOption', () => {
357+
const AppendJimp = configure({});
358+
359+
AppendJimp.appendConstructorOption(
360+
'Name of Option',
361+
args => args.hasSomeCustomThing,
362+
function(resolve, reject, args) {
363+
// $ExpectError
364+
this.bitmap = 3;
365+
// $ExpectError
366+
AppendJimp.resize(2, 2);
367+
resolve();
368+
}
369+
);
370+
});

packages/jimp/types/index.d.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -341,14 +341,19 @@ interface DepreciatedJimp {
341341
mask(src: this, x: number, y: number, cb?: ImageCallback): this;
342342

343343
// Functions
344-
appendConstructorOption<T extends any[]>(
344+
/**
345+
* I'd like to make `Args` generic and used in `run` and `test` but alas,
346+
* it's not possible RN:
347+
* https://github.com/microsoft/TypeScript/issues/26113
348+
*/
349+
appendConstructorOption<Args extends any[]>(
345350
name: string,
346-
test: (...args: T[]) => boolean,
351+
test: (...args: any[]) => boolean,
347352
run: (
348353
this: this,
349-
resolve: (jimp: this) => any,
354+
resolve: (jimp?: this) => any,
350355
reject: (reason: Error) => any,
351-
...args: T[]
356+
...args: any[]
352357
) => any
353358
): void;
354359
read(path: string, cb?: ImageCallback): Promise<this>;

packages/jimp/types/test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,16 @@ test('Can handle callback with constructor', () => {
8686
cbJimpInst.func();
8787
});
8888
})
89+
90+
test('Can handle appendConstructorOption', () => {
91+
Jimp.appendConstructorOption(
92+
'Name of Option',
93+
args => args.hasSomeCustomThing,
94+
function(resolve, reject, args) {
95+
// $ExpectError
96+
this.bitmap = 3;
97+
Jimp.resize(2, 2);
98+
resolve();
99+
}
100+
);
101+
});

0 commit comments

Comments
 (0)