From 01c6dc0150f0ea5e2499bffb12d099fcd27ef910 Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Tue, 10 Sep 2019 16:57:32 -0700 Subject: [PATCH] Added initial 3.1 types --- package.json | 2 +- packages/jimp/package.json | 7 + packages/jimp/types/index.d.ts | 605 +++++++++++++++++++++++- packages/jimp/types/ts3.1/index.d.ts | 29 ++ packages/jimp/types/ts3.1/test.ts | 14 + packages/jimp/types/ts3.1/tsconfig.json | 18 + 6 files changed, 651 insertions(+), 24 deletions(-) create mode 100644 packages/jimp/types/ts3.1/index.d.ts create mode 100644 packages/jimp/types/ts3.1/test.ts create mode 100644 packages/jimp/types/ts3.1/tsconfig.json diff --git a/package.json b/package.json index ef2ae0608..b27290776 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "build:watch": "lerna run build:watch --parallel", "release": "auto shipit", "tsTest:custom": "dtslint packages/custom/types --expectOnly", - "tsTest:main": "dtslint packages/jimp/types --expectOnly " + "tsTest:main": "dtslint packages/jimp/types --expectOnly" }, "devDependencies": { "@babel/cli": "^7.1.0", diff --git a/packages/jimp/package.json b/packages/jimp/package.json index b54c8f994..24b4f4a42 100644 --- a/packages/jimp/package.json +++ b/packages/jimp/package.json @@ -6,6 +6,13 @@ "module": "es/index.js", "browser": "browser/lib/jimp.js", "types": "types/index.d.ts", + "typesVersions": { + ">=3.1.0-0": { + "*": [ + "types/ts3.1/index.d.ts" + ] + } + }, "tonicExampleFilename": "example.js", "files": [ "browser", diff --git a/packages/jimp/types/index.d.ts b/packages/jimp/types/index.d.ts index 9a17f4343..74f0dc9da 100644 --- a/packages/jimp/types/index.d.ts +++ b/packages/jimp/types/index.d.ts @@ -1,31 +1,590 @@ -// TypeScript Version: 3.1 +// TypeScript Version: 2.8 + /** - * While there is nothing in these typings that prevent it from running in TS 2.8 even, - * due to the complexity of the typings anything lower than TS 3.1 will only see - * Jimp as `any`. In order to test the strict versions of these types in our typing - * test suite, the version has been bumped to 3.1 + * @deprecated Jimp typings for TS <3.1 are being depreciated. Please upgrade your TypeScript version */ +declare class Jimp { + // Constructors + constructor(path: string, cb?: ImageCallback); + constructor(urlOptions: URLOptions, cb?: ImageCallback); + constructor(image: Jimp, cb?: ImageCallback); + constructor(data: Buffer | Bitmap, cb?: ImageCallback); + constructor(w: number, h: number, cb?: ImageCallback); + constructor( + w: number, + h: number, + background?: number | string, + cb?: ImageCallback + ); + // For custom constructors when using Jimp.appendConstructorOption + constructor(...args: any[]); + prototype: Jimp; + + // Constants + AUTO: -1; + + // supported mime types + MIME_PNG: 'image/png'; + MIME_TIFF: 'image/tiff'; + MIME_JPEG: 'image/jpeg'; + MIME_JGD: 'image/jgd'; + MIME_BMP: 'image/bmp'; + MIME_X_MS_BMP: 'image/x-ms-bmp'; + MIME_GIF: 'image/gif'; + // PNG filter types + PNG_FILTER_AUTO: -1; + PNG_FILTER_NONE: 0; + PNG_FILTER_SUB: 1; + PNG_FILTER_UP: 2; + PNG_FILTER_AVERAGE: 3; + PNG_FILTER_PATH: 4; + + // resize methods + RESIZE_NEAREST_NEIGHBOR: 'nearestNeighbor'; + RESIZE_BILINEAR: 'bilinearInterpolation'; + RESIZE_BICUBIC: 'bicubicInterpolation'; + RESIZE_HERMITE: 'hermiteInterpolation'; + RESIZE_BEZIER: 'bezierInterpolation'; + + // blend modes + BLEND_SOURCE_OVER: string; + BLEND_DESTINATION_OVER: string; + BLEND_MULTIPLY: string; + BLEND_SCREEN: string; + BLEND_OVERLAY: string; + BLEND_DARKEN: string; + BLEND_LIGHTEN: string; + BLEND_HARDLIGHT: string; + BLEND_DIFFERENCE: string; + BLEND_EXCLUSION: string; + + // Align modes for cover, contain, bit masks + HORIZONTAL_ALIGN_LEFT: 1; + HORIZONTAL_ALIGN_CENTER: 2; + HORIZONTAL_ALIGN_RIGHT: 4; + + VERTICAL_ALIGN_TOP: 8; + VERTICAL_ALIGN_MIDDLE: 16; + VERTICAL_ALIGN_BOTTOM: 32; + + // Font locations + FONT_SANS_8_BLACK: string; + FONT_SANS_10_BLACK: string; + FONT_SANS_12_BLACK: string; + FONT_SANS_14_BLACK: string; + FONT_SANS_16_BLACK: string; + FONT_SANS_32_BLACK: string; + FONT_SANS_64_BLACK: string; + FONT_SANS_128_BLACK: string; + + FONT_SANS_8_WHITE: string; + FONT_SANS_16_WHITE: string; + FONT_SANS_32_WHITE: string; + FONT_SANS_64_WHITE: string; + FONT_SANS_128_WHITE: string; + + // Edge Handling + EDGE_EXTEND: 1; + EDGE_WRAP: 2; + EDGE_CROP: 3; + + // Properties + bitmap: Bitmap; + + _quality: number; + _deflateLevel: number; + _deflateStrategy: number; + _filterType: number; + _rgba: boolean; + _background: number; + _originalMime: string; + + // Methods + on( + event: T, + cb: (data: ListenerData) => any + ): any; + parseBitmap( + data: Buffer, + path: string | null | undefined, + cb?: ImageCallback + ): void; + hasAlpha(): boolean; + getHeight(): number; + getWidth(): number; + inspect(): string; + toString(): string; + getMIME(): string; + getExtension(): string; + distanceFromHash(hash: string): number; + write(path: string, cb?: ImageCallback): this; + writeAsync(path: string): Promise; + deflateLevel(l: number, cb?: ImageCallback): this; + deflateStrategy(s: number, cb?: ImageCallback): this; + colorType(s: number, cb?: ImageCallback): this; + filterType(f: number, cb?: ImageCallback): this; + rgba(bool: boolean, cb?: ImageCallback): this; + quality(n: number, cb?: ImageCallback): this; + getBase64(mime: string, cb: GenericCallback): this; + getBase64Async(mime: string): Promise; + hash(cb?: GenericCallback): string; + hash( + base: number | null | undefined, + cb?: GenericCallback + ): string; + getBuffer(mime: string, cb: GenericCallback): this; + getBufferAsync(mime: string): Promise; + getPixelIndex( + x: number, + y: number, + cb?: GenericCallback + ): number; + getPixelIndex( + x: number, + y: number, + edgeHandling: string, + cb?: GenericCallback + ): number; + getPixelColor( + x: number, + y: number, + cb?: GenericCallback + ): number; + getPixelColour( + x: number, + y: number, + cb?: GenericCallback + ): number; + setPixelColor(hex: number, x: number, y: number, cb?: ImageCallback): this; + setPixelColour(hex: number, x: number, y: number, cb?: ImageCallback): this; + clone(cb?: ImageCallback): this; + cloneQuiet(cb?: ImageCallback): this; + background(hex: number, cb?: ImageCallback): this; + backgroundQuiet(hex: number, cb?: ImageCallback): this; + scan( + x: number, + y: number, + w: number, + h: number, + f: (this: this, x: number, y: number, idx: number) => any, + cb?: ImageCallback + ): this; + scanQuiet( + x: number, + y: number, + w: number, + h: number, + f: (this: this, x: number, y: number, idx: number) => any, + cb?: ImageCallback + ): this; + scanIterator( + x: number, + y: number, + w: number, + h: number + ): IterableIterator<{ x: number; y: number; idx: number; image: Jimp }>; + crop(x: number, y: number, w: number, h: number, cb?: ImageCallback): this; + cropQuiet( + x: number, + y: number, + w: number, + h: number, + cb?: ImageCallback + ): this; + + // Color methods + brightness(val: number, cb?: ImageCallback): this; + contrast(val: number, cb?: ImageCallback): this; + posterize(n: number, cb?: ImageCallback): this; + greyscale(cb?: ImageCallback): this; + grayscale(cb?: ImageCallback): this; + opacity(f: number, cb?: ImageCallback): this; + sepia(cb?: ImageCallback): this; + fade(f: number, cb?: ImageCallback): this; + convolution(kernel: number[][], cb?: ImageCallback): this; + convolution( + kernel: number[][], + edgeHandling: string, + cb?: ImageCallback + ): this; + opaque(cb?: ImageCallback): this; + pixelate(size: number, cb?: ImageCallback): this; + pixelate( + size: number, + x: number, + y: number, + w: number, + h: number, + cb?: ImageCallback + ): this; + convolute(kernel: number[][], cb?: ImageCallback): this; + convolute( + kernel: number[][], + x: number, + y: number, + w: number, + h: number, + cb?: ImageCallback + ): this; + color(actions: ColorAction[], cb?: ImageCallback): this; + colour(actions: ColorAction[], cb?: ImageCallback): this; + + // Shape methods + rotate(deg: number, cb?: ImageCallback): this; + rotate(deg: number, mode: string | boolean, cb?: ImageCallback): this; + flip(horizontal: boolean, vertical: boolean, cb?: ImageCallback): this; + mirror(horizontal: boolean, vertical: boolean, cb?: ImageCallback): this; + resize(w: number, h: number, cb?: ImageCallback): this; + resize(w: number, h: number, mode?: string, cb?: ImageCallback): this; + cover(w: number, h: number, cb?: ImageCallback): this; + cover(w: number, h: number, alignBits?: number, cb?: ImageCallback): this; + cover( + w: number, + h: number, + alignBits?: number, + mode?: string, + cb?: ImageCallback + ): this; + contain(w: number, h: number, cb?: ImageCallback): this; + contain(w: number, h: number, mode?: string, cb?: ImageCallback): this; + contain(w: number, h: number, alignBits?: number, cb?: ImageCallback): this; + contain( + w: number, + h: number, + alignBits?: number, + mode?: string, + cb?: ImageCallback + ): this; + scale(f: number, cb?: ImageCallback): this; + scale(f: number, mode?: string, cb?: ImageCallback): this; + scaleToFit(w: number, h: number, cb?: ImageCallback): this; + scaleToFit(w: number, h: number, mode?: string, cb?: ImageCallback): this; + displace(map: Jimp, offset: number, cb?: ImageCallback): this; + autocrop(tolerance?: number, cb?: ImageCallback): this; + autocrop(cropOnlyFrames?: boolean, cb?: ImageCallback): this; + autocrop( + tolerance?: number, + cropOnlyFrames?: boolean, + cb?: ImageCallback + ): this; + autocrop( + options: { + tolerance?: number; + cropOnlyFrames?: boolean; + cropSymmetric?: boolean; + leaveBorder?: number; + }, + cb?: ImageCallback + ): this; + + // Text methods + print( + font: Font, + x: number, + y: number, + text: PrintableText, + cb?: ImageCallback + ): this; + print( + font: Font, + x: number, + y: number, + text: PrintableText, + maxWidth?: number, + cb?: ImageCallback + ): this; + print( + font: Font, + x: number, + y: number, + text: PrintableText, + maxWidth?: number, + maxHeight?: number, + cb?: ImageCallback + ): this; + + // Effect methods + blur(r: number, cb?: ImageCallback): this; + dither565(cb?: ImageCallback): this; + dither16(cb?: ImageCallback): this; + histogram(): { + r: number[]; + g: number[]; + b: number[]; + }; + normalize(cb?: ImageCallback): this; + invert(cb?: ImageCallback): this; + gaussian(r: number, cb?: ImageCallback): this; + composite( + src: Jimp, + x: number, + y: number, + options?: BlendMode, + cb?: ImageCallback + ): this; + blit(src: Jimp, x: number, y: number, cb?: ImageCallback): this; + blit( + src: Jimp, + x: number, + y: number, + srcx: number, + srcy: number, + srcw: number, + srch: number, + cb?: ImageCallback + ): this; + mask(src: Jimp, x: number, y: number, cb?: ImageCallback): this; + + // Functions + appendConstructorOption( + name: string, + test: (...args: T[]) => boolean, + run: ( + this: Jimp, + resolve: (jimp: Jimp) => any, + reject: (reason: Error) => any, + ...args: T[] + ) => any + ): void; + read(path: string): Promise; + read(image: Jimp): Promise; + read(data: Buffer): Promise; + read(w: number, h: number, background?: number | string): Promise; + create(path: string): Promise; + create(image: Jimp): Promise; + create(data: Buffer): Promise; + create(w: number, h: number, background?: number | string): Promise; + rgbaToInt( + r: number, + g: number, + b: number, + a: number, + cb: GenericCallback + ): number; + intToRGBA(i: number, cb?: GenericCallback): RGBA; + cssColorToHex(cssColor: string): number; + limit255(n: number): number; + diff( + img1: Jimp, + img2: Jimp, + threshold?: number + ): { + percent: number; + image: Jimp; + }; + distance(img1: Jimp, img2: Jimp): number; + compareHashes(hash1: string, hash2: string): number; + colorDiff(rgba1: RGB, rgba2: RGB): number; + colorDiff(rgba1: RGBA, rgba2: RGBA): number; + loadFont(file: string): Promise; + loadFont(file: string, cb: GenericCallback): Promise; + measureText(font: Font, text: PrintableText): number; + measureTextHeight(font: Font, text: PrintableText, maxWidth: number): number; + circle( + options?: { + radius: number; + x: number; + y: number; + }, + cb?: ImageCallback + ): this; + circle(cb?: ImageCallback): this; + fishEye(opts?: { r: number }, cb?: ImageCallback): this; + fishEye(cb?: ImageCallback): this; + shadow( + options?: { + size?: number; + opacity?: number; + x?: number; + y?: number; + }, + cb?: ImageCallback + ): this; + shadow(cb?: ImageCallback): this; + threshold( + opts: { + max: number; + replace?: number; + autoGreyscale?: boolean; + }, + cb?: ImageCallback + ): this; +} + +declare const JimpInst: Jimp; + +export default JimpInst; + +type GenericCallback = ( + this: TThis, + err: Error | null, + value: T +) => U; + +type ImageCallback = ( + this: Jimp, + err: Error | null, + value: Jimp, + coords: { + x: number; + y: number; + } +) => U; -import { - Jimp as JimpType, - Bitmap, - RGB, - RGBA, - WellFormedValues, - UnionToIntersection, - GetPluginVal -} from '@jimp/core'; -import typeFn from '@jimp/types'; -import pluginFn from '@jimp/plugins'; +type ColorActionName = + | 'mix' + | 'tint' + | 'shade' + | 'xor' + | 'red' + | 'green' + | 'blue' + | 'hue'; -type Types = ReturnType; -type Plugins = ReturnType; +type ColorAction = { + apply: ColorActionName; + params: any; +}; +type BlendMode = { + mode: string; + opacitySource: number; + opacityDest: number; +}; -export { Bitmap, RGB, RGBA }; +type ChangeName = 'background' | 'scan' | 'crop'; -export { FontChar, FontInfo, FontCommon, Font } from '@jimp/plugin-print'; +type ListenableName = + | 'any' + | 'initialized' + | 'before-change' + | 'changed' + | 'before-clone' + | 'cloned' + | ChangeName; -export type FullJimpType = JimpType & UnionToIntersection> & UnionToIntersection>; -declare const Jimp: FullJimpType; -export default Jimp; +type ListenerData = T extends 'any' + ? any + : T extends ChangeName + ? { + eventName: 'before-change' | 'changed'; + methodName: T; + [key: string]: any; + } + : { + eventName: T; + methodName: T extends 'initialized' + ? 'constructor' + : T extends 'before-change' | 'changed' + ? ChangeName + : T extends 'before-clone' | 'cloned' ? 'clone' : any; + }; + +type PrintableText = + | any + | { + text: string; + alignmentX: number; + alignmentY: number; + }; + +type URLOptions = { + url: string; + compression?: boolean; + headers: { + [key: string]: any; + }; +}; + +/** + * @deprecated Jimp typings for TS <3.1 are being depreciated. Please upgrade your TypeScript version + */ +export interface Bitmap { + data: Buffer; + width: number; + height: number; +} +/** + * @deprecated Jimp typings for TS <3.1 are being depreciated. Please upgrade your TypeScript version + */ +export interface RGB { + r: number; + g: number; + b: number; +} + +/** + * @deprecated Jimp typings for TS <3.1 are being depreciated. Please upgrade your TypeScript version + */ +export interface RGBA { + r: number; + g: number; + b: number; + a: number; +} + +/** + * @deprecated Jimp typings for TS <3.1 are being depreciated. Please upgrade your TypeScript version + */ +export interface FontChar { + id: number; + x: number; + y: number; + width: number; + height: number; + xoffset: number; + yoffset: number; + xadvance: number; + page: number; + chnl: number; +} + +/** + * @deprecated Jimp typings for TS <3.1 are being depreciated. Please upgrade your TypeScript version + */ +export interface FontInfo { + face: string; + size: number; + bold: number; + italic: number; + charset: string; + unicode: number; + stretchH: number; + smooth: number; + aa: number; + padding: [number, number, number, number]; + spacing: [number, number]; +} + +/** + * @deprecated Jimp typings for TS <3.1 are being depreciated. Please upgrade your TypeScript version + */ +export interface FontCommon { + lineHeight: number; + base: number; + scaleW: number; + scaleH: number; + pages: number; + packed: number; + alphaChnl: number; + redChnl: number; + greenChnl: number; + blueChnl: number; +} + +/** + * @deprecated Jimp typings for TS <3.1 are being depreciated. Please upgrade your TypeScript version + */ +export interface Font { + chars: { + [char: string]: FontChar; + }; + kernings: { + [firstString: string]: { + [secondString: string]: number; + }; + }; + pages: string[]; + common: FontCommon; + info: FontInfo; +} diff --git a/packages/jimp/types/ts3.1/index.d.ts b/packages/jimp/types/ts3.1/index.d.ts new file mode 100644 index 000000000..8125aeb7b --- /dev/null +++ b/packages/jimp/types/ts3.1/index.d.ts @@ -0,0 +1,29 @@ +/** + * While there is nothing in these typings that prevent it from running in TS 2.8 even, + * due to the complexity of the typings anything lower than TS 3.1 will only see + * Jimp as `any`. In order to test the strict versions of these types in our typing + * test suite, the version has been bumped to 3.1 + */ + +import { + Jimp as JimpType, + Bitmap, + RGB, + RGBA, + WellFormedValues, + UnionToIntersection, + GetPluginVal +} from '@jimp/core'; +import typeFn from '@jimp/types'; +import pluginFn from '@jimp/plugins'; + +type Types = ReturnType; +type Plugins = ReturnType; + +export { Bitmap, RGB, RGBA }; + +export { FontChar, FontInfo, FontCommon, Font } from '@jimp/plugin-print'; + +export type FullJimpType = JimpType & UnionToIntersection> & UnionToIntersection>; +declare const Jimp: FullJimpType; +export default Jimp; diff --git a/packages/jimp/types/ts3.1/test.ts b/packages/jimp/types/ts3.1/test.ts new file mode 100644 index 000000000..67220b5c7 --- /dev/null +++ b/packages/jimp/types/ts3.1/test.ts @@ -0,0 +1,14 @@ +import Jimp from 'jimp'; + +// Main Jimp export should already have all of these already applied +Jimp.read('Test'); +Jimp.displace(Jimp, 2); +Jimp.resize(40, 40); +// $ExpectType 0 +Jimp.PNG_FILTER_NONE; + +// $ExpectError +Jimp.test; + +// $ExpectError +Jimp.func(); diff --git a/packages/jimp/types/ts3.1/tsconfig.json b/packages/jimp/types/ts3.1/tsconfig.json new file mode 100644 index 000000000..6dbbf36d9 --- /dev/null +++ b/packages/jimp/types/ts3.1/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "noEmit": true, + + // If the library is an external module (uses `export`), this allows your test file to import "mylib" instead of "./index". + // If the library is global (cannot be imported via `import` or `require`), leave this out. + "baseUrl": "../test", + "paths": { + "mylib": ["."] + } + } +}