Skip to content

Commit

Permalink
Fix issues with typings using classes, publish @core typings, and fix…
Browse files Browse the repository at this point in the history
… 3.1 typings (#792)

* Includes `types` folder for jimp and @jimp/core for builds

* Revert main `jimp` package to use more similar typing to 0.7, remove TS3.1 from packagejson

* Fix issues with classes used within Jimp

* Simplify custom types

* Added tests for and fixed class usage in custom

* Simplified custom and core typings by moving constructors to new interface

* Added back 3.1 typings, fixed them, changed back to ES6 export

* Remove unusable usages from prior TS docs

* Added note about import differences

* Add tests against Jimp instance cloning, fix types

This requires us to remove 3.1 typings, left a note in the 3.1 tests on how to enable them again when things are working properly.
  • Loading branch information
crutchcorn authored and hipstersmoothie committed Sep 18, 2019
1 parent c4575b6 commit e4bb762
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 130 deletions.
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"dist",
"es",
"index.d.ts",
"fonts"
"fonts",
"types"
],
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export * from './etc';
export * from './functions';
export * from './plugins';
export * from './utils';
import {Jimp} from './jimp';
import {Jimp, JimpConstructors} from './jimp';

export { Jimp };
export { Jimp, JimpConstructors };
declare const defaultExp: Jimp;
export default defaultExp;
30 changes: 16 additions & 14 deletions packages/core/types/jimp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ import {
RGB
} from './etc';

export declare class Jimp {
// Constructors
constructor(path: string, cb?: ImageCallback);
constructor(urlOptions: URLOptions, cb?: ImageCallback);
constructor(image: Jimp, cb?: ImageCallback);
constructor(data: Buffer, cb?: ImageCallback);
constructor(data: Bitmap, cb?: ImageCallback);
constructor(w: number, h: number, cb?: ImageCallback);
constructor(
export interface JimpConstructors {
new(path: string, cb?: ImageCallback): this;
new(urlOptions: URLOptions, cb?: ImageCallback): this;
new(image: Jimp, cb?: ImageCallback): this;
new(data: Buffer, cb?: ImageCallback): this;
new(data: Bitmap, cb?: ImageCallback): this;
new(w: number, h: number, cb?: ImageCallback): this;
new(
w: number,
h: number,
background?: number | string,
cb?: ImageCallback
);
): this;
// For custom constructors when using Jimp.appendConstructorOption
constructor(...args: any[]);
new(...args: any[]): this;
}

export interface Jimp extends JimpConstructors {
prototype: this;
// Constants
AUTO: -1;
Expand Down Expand Up @@ -75,7 +77,7 @@ export declare class Jimp {
getExtension(): string;
distanceFromHash(hash: string): number;
write(path: string, cb?: ImageCallback): this;
writeAsync(path: string): Promise<Jimp>;
writeAsync(path: string): Promise<this>;
rgba(bool: boolean, cb?: ImageCallback): this;
getBase64(mime: string, cb: GenericCallback<string, any, this>): this;
getBase64Async(mime: string): Promise<string>;
Expand Down Expand Up @@ -150,8 +152,8 @@ export declare class Jimp {
name: string,
test: (...args: T[]) => boolean,
run: (
this: Jimp,
resolve: (jimp: Jimp) => any,
this: this,
resolve: (jimp: this) => any,
reject: (reason: Error) => any,
...args: T[]
) => any
Expand Down
45 changes: 15 additions & 30 deletions packages/custom/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,29 @@ import {
Jimp,
JimpPlugin,
JimpType,
GetIntersectionFromPlugins
GetIntersectionFromPlugins,
JimpConstructors
} from '@jimp/core';

declare function configure<
PluginFuncArr extends FunctionRet<JimpPlugin>,
JimpInstance extends Jimp = Jimp
>(
configuration: {
plugins: PluginFuncArr;
},
jimpInstance?: JimpInstance
): Exclude<JimpInstance, undefined> &
GetIntersectionFromPlugins<PluginFuncArr>;

declare function configure<
TypesFuncArr extends FunctionRet<JimpType>,
JimpInstance extends Jimp = Jimp
>(
configuration: {
types: TypesFuncArr;
},
jimpInstance?: JimpInstance
): Exclude<JimpInstance, undefined> &
GetIntersectionFromPlugins<TypesFuncArr>;
type JimpInstance<
TypesFuncArr extends FunctionRet<JimpType> | undefined,
PluginFuncArr extends FunctionRet<JimpPlugin> | undefined,
J extends Jimp
> = Exclude<J, undefined> &
GetIntersectionFromPlugins<Exclude<TypesFuncArr | PluginFuncArr, undefined>> &
JimpConstructors;

declare function configure<
TypesFuncArr extends FunctionRet<JimpType>,
PluginFuncArr extends FunctionRet<JimpPlugin>,
JimpInstance extends Jimp = Jimp
TypesFuncArr extends FunctionRet<JimpType> | undefined = undefined,
PluginFuncArr extends FunctionRet<JimpPlugin> | undefined = undefined,
J extends Jimp = Jimp
>(
configuration: {
types?: TypesFuncArr;
plugins?: PluginFuncArr;
},
jimpInstance?: JimpInstance
jimpInstance?: J
// Since JimpInstance is required, we want to use the default `Jimp` type
): Exclude<JimpInstance, undefined> &
GetIntersectionFromPlugins<TypesFuncArr> &
GetIntersectionFromPlugins<PluginFuncArr>;
): JimpInstance<TypesFuncArr, PluginFuncArr, J>;

export default configure;
export default configure;
176 changes: 150 additions & 26 deletions packages/custom/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,56 @@ const CustomJimp = configure({
plugins: [displace, resize]
});

// Methods from types should be applied
CustomJimp.deflateLevel(4);
// Constants from types should be applied
// $ExpectType 0
CustomJimp.PNG_FILTER_NONE;
test('can handle custom jimp', () => {
// Methods from types should be applied
CustomJimp.deflateLevel(4);
// Constants from types should be applied
// $ExpectType 0
CustomJimp.PNG_FILTER_NONE;

// Core functions should still work from Jimp
CustomJimp.read('Test');

// Constants should be applied from ill-formed plugins
CustomJimp.displace(CustomJimp, 2);

// Methods should be applied from well-formed plugins
CustomJimp.resize(40, 40)

// Constants should be applied from well-formed plugins
CustomJimp.RESIZE_NEAREST_NEIGHBOR

// $ExpectError
CustomJimp.test;

// $ExpectError
CustomJimp.func();

// Core functions should still work from Jimp
CustomJimp.read('Test');
const Jiimp = new CustomJimp('test');
// Methods from types should be applied
Jiimp.deflateLevel(4);
// Constants from types should be applied
// $ExpectType 0
Jiimp.PNG_FILTER_NONE;

// Constants should be applied from ill-formed plugins
CustomJimp.displace(CustomJimp, 2);
// Core functions should still work from Jimp
Jiimp.read('Test');

// Methods should be applied from well-formed plugins
CustomJimp.resize(40, 40)
// Constants should be applied from ill-formed plugins
Jiimp.displace(Jiimp, 2);

// Constants should be applied from well-formed plugins
CustomJimp.RESIZE_NEAREST_NEIGHBOR
// Methods should be applied from well-formed plugins
Jiimp.resize(40, 40)

// Constants should be applied from well-formed plugins
Jiimp.RESIZE_NEAREST_NEIGHBOR

// $ExpectError
CustomJimp.test;
// $ExpectError
Jiimp.test;

// $ExpectError
CustomJimp.func();
// $ExpectError
Jiimp.func();
});

test('can compose', () => {
const OtherCustomJimp = configure({
Expand Down Expand Up @@ -68,6 +95,31 @@ test('can compose', () => {

// $ExpectError
OtherCustomJimp.func();

const Jiimp = new OtherCustomJimp('test');
// Methods from types should be applied
Jiimp.deflateLevel(4);
// Constants from types should be applied
// $ExpectType 0
Jiimp.PNG_FILTER_NONE;

// Core functions should still work from Jimp
Jiimp.read('Test');

// Constants should be applied from ill-formed plugins
Jiimp.displace(Jiimp, 2);

// Methods should be applied from well-formed plugins
Jiimp.resize(40, 40)

// Constants should be applied from well-formed plugins
Jiimp.RESIZE_NEAREST_NEIGHBOR

// $ExpectError
Jiimp.test;

// $ExpectError
Jiimp.func();
});

test('can handle only plugins', () => {
Expand All @@ -93,6 +145,26 @@ test('can handle only plugins', () => {

// $ExpectError
PluginsJimp.func();

const Jiimp = new PluginsJimp('test');

// Core functions should still work from Jimp
Jiimp.read('Test');

// Constants should be applied from ill-formed plugins
Jiimp.displace(Jiimp, 2);

// Methods should be applied from well-formed plugins
Jiimp.resize(40, 40)

// Constants should be applied from well-formed plugins
Jiimp.RESIZE_NEAREST_NEIGHBOR

// $ExpectError
Jiimp.test;

// $ExpectError
Jiimp.func();
})

test('can handle only all types', () => {
Expand All @@ -103,7 +175,6 @@ test('can handle only all types', () => {
// Methods from types should be applied
TypesJimp.filterType(4);
// Constants from types should be applied
// Commented for complexity errors
// $ExpectType 0
TypesJimp.PNG_FILTER_NONE;

Expand All @@ -112,15 +183,31 @@ test('can handle only all types', () => {

// $ExpectError
TypesJimp.func();

const Jiimp = new TypesJimp('test');
// Methods from types should be applied
Jiimp.filterType(4);
// Constants from types should be applied
// $ExpectType 0
Jiimp.PNG_FILTER_NONE;

// $ExpectError
Jiimp.test;

// $ExpectError
Jiimp.func();
});

test('can handle only one type', () => {
const PngJimp = configure({
types: [png]
});

// Constants from other types should be not applied
// $ExpectError
PngJimp.MIME_TIFF;

// Constants from types should be applied
// Commented for complexity errors
// $ExpectType 0
PngJimp.PNG_FILTER_NONE;

Expand All @@ -129,24 +216,61 @@ test('can handle only one type', () => {

// $ExpectError
PngJimp.func();


const Jiimp = new PngJimp('test');
// Constants from other types should be not applied
// $ExpectError
Jiimp.MIME_TIFF;

// Constants from types should be applied
// $ExpectType 0
Jiimp.PNG_FILTER_NONE;

// $ExpectError
Jiimp.test;

// $ExpectError
Jiimp.func();
});


test('can handle only one plugin', () => {
const PngJimp = configure({
const ResizeJimp = configure({
plugins: [resize]
});

// Constants from types should be applied
// Commented for complexity errors
// Constants from other plugins should be not applied
// $ExpectError
ResizeJimp.FONT_SANS_8_BLACK;

// Constants from plugin should be applied
// $ExpectType "nearestNeighbor"
PngJimp.RESIZE_NEAREST_NEIGHBOR;
ResizeJimp.RESIZE_NEAREST_NEIGHBOR;

PngJimp.resize(2, 2);
ResizeJimp.resize(2, 2);

// $ExpectError
PngJimp.test;
ResizeJimp.test;

// $ExpectError
PngJimp.func();
ResizeJimp.func();


const Jiimp: typeof ResizeJimp = new ResizeJimp('test');
// Constants from other plugins should be not applied
// $ExpectError
Jiimp.FONT_SANS_8_BLACK;

// Constants from plugin should be applied
// $ExpectType "nearestNeighbor"
Jiimp.RESIZE_NEAREST_NEIGHBOR;

Jiimp.resize(2, 2);

// $ExpectError
Jiimp.test;

// $ExpectError
Jiimp.func();
});
Loading

0 comments on commit e4bb762

Please sign in to comment.