diff --git a/types/d/auto-bind.d.ts b/types/d/auto-bind.d.ts new file mode 100644 index 0000000000..f52f8a955d --- /dev/null +++ b/types/d/auto-bind.d.ts @@ -0,0 +1,13 @@ +export = autoBind; + +declare function autoBind( + obj: { [key: string]: PropertyDescriptor }, + options?: autoBind.Options +): PropertyDescriptorMap; + +declare namespace autoBind { + interface Options { + overwriteDefinition?: boolean; + resolveContext?: (context: any) => any; + } +} diff --git a/types/d/d-tests.ts b/types/d/d-tests.ts new file mode 100644 index 0000000000..e08582cf0e --- /dev/null +++ b/types/d/d-tests.ts @@ -0,0 +1,85 @@ +import d = require('d'); +import autoBind = require('d/auto-bind'); +import lazy = require('d/lazy'); + +class Account {} +Object.defineProperties(Account.prototype, { + deposit: d(() => {}), + withdraw: d(() => {}), + balance: d.gs(() => {}), +}); + +d('foo'); // $ExpectType PropertyDescriptor +d('foo', { enumerable: true }); // $ExpectType PropertyDescriptor +d('c', 'foo'); // $ExpectType PropertyDescriptor +d('c', 'foo', { enumerable: true }); // $ExpectType PropertyDescriptor + +d('c', 'foo'); // $ExpectType PropertyDescriptor +d('e', 'foo'); // $ExpectType PropertyDescriptor +d('w', 'foo'); // $ExpectType PropertyDescriptor +d('ce', 'foo'); // $ExpectType PropertyDescriptor +d('cw', 'foo'); // $ExpectType PropertyDescriptor +d('ew', 'foo'); // $ExpectType PropertyDescriptor +d('cew', 'foo'); // $ExpectType PropertyDescriptor +d('foo', 'foo'); // $ExpectError + +d.gs('c', { enumerable: true }); // $ExpectType PropertyDescriptor +d.gs('c', () => ({}), { enumerable: true }); // $ExpectType PropertyDescriptor +d.gs(() => ({})); // $ExpectType PropertyDescriptor +d.gs(null, () => ({})); // $ExpectType PropertyDescriptor +d.gs(undefined, () => ({})); // $ExpectType PropertyDescriptor +d.gs('c', () => ({})); // $ExpectType PropertyDescriptor +d.gs('c', null, () => ({})); // $ExpectType PropertyDescriptor +d.gs('c', undefined, () => ({})); // $ExpectType PropertyDescriptor +d.gs('c', null, () => ({}), { enumerable: true }); // $ExpectType PropertyDescriptor +d.gs('c', undefined, () => ({}), { enumerable: true }); // $ExpectType PropertyDescriptor + +d.gs('c', () => ({})); // $ExpectType PropertyDescriptor +d.gs('e', () => ({})); // $ExpectType PropertyDescriptor +d.gs('ce', () => ({})); // $ExpectType PropertyDescriptor +d.gs('cew', () => ({})); // $ExpectError + +class Foo { + _count: number; +} + +Object.defineProperties( + Foo.prototype, + autoBind({ + increment: d(function(this: any) { + ++this._count; + }), + }) +); +autoBind( + { + increment: d(function(this: any) { + ++this._count; + }), + }, + { overwriteDefinition: true } +); +autoBind( + { + increment: d(function(this: any) { + ++this._count; + }), + }, + { + resolveContext(ctx: any) { + return ctx; + }, + } +); + +Object.defineProperties( + Foo.prototype, + lazy({ + items: d(() => { + return []; + }), + }) +); + +const foo = new Foo(); +(foo as any).items.push(1, 2); // foo.items array created and defined directly on foo diff --git a/types/d/index.d.ts b/types/d/index.d.ts new file mode 100644 index 0000000000..9130000999 --- /dev/null +++ b/types/d/index.d.ts @@ -0,0 +1,40 @@ +// Type definitions for d 1.0 +// Project: https://github.com/medikoo/d#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +export = d; + +declare function d(value: any, options?: PropertyDescriptor): PropertyDescriptor; +declare function d(flags: d.Flags, value: any, options?: PropertyDescriptor): PropertyDescriptor; + +declare namespace d { + function gs(flags: GetSetFlags, options: PropertyDescriptor): PropertyDescriptor; + function gs(flags: GetSetFlags, get: (...args: any[]) => any, options: PropertyDescriptor): PropertyDescriptor; + function gs( + get: (...args: any[]) => any, + set?: ((...args: any[]) => any) | null, + options?: PropertyDescriptor + ): PropertyDescriptor; + function gs( + get: ((...args: any[]) => any) | null | undefined, + set: (...args: any[]) => any, + options?: PropertyDescriptor + ): PropertyDescriptor; + function gs( + flags: GetSetFlags, + get: (...args: any[]) => any, + set?: ((...args: any[]) => any) | null, + options?: PropertyDescriptor + ): PropertyDescriptor; + function gs( + flags: GetSetFlags, + get: ((...args: any[]) => any) | null | undefined, + set: (...args: any[]) => any, + options?: PropertyDescriptor + ): PropertyDescriptor; + + type GetSetFlags = 'c' | 'e' | 'ce'; + type Flags = GetSetFlags | 'w' | 'cw' | 'ew' | 'cew'; +} diff --git a/types/d/lazy.d.ts b/types/d/lazy.d.ts new file mode 100644 index 0000000000..3cba797050 --- /dev/null +++ b/types/d/lazy.d.ts @@ -0,0 +1,3 @@ +export = lazy; + +declare function lazy(obj: { [key: string]: PropertyDescriptor }): PropertyDescriptorMap; diff --git a/types/d/tsconfig.json b/types/d/tsconfig.json new file mode 100644 index 0000000000..26c1a65f3e --- /dev/null +++ b/types/d/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "auto-bind.d.ts", + "lazy.d.ts", + "d-tests.ts" + ] +} diff --git a/types/d/tslint.json b/types/d/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/d/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }