Merge pull request #31490 from BendingBender/d

[d] Add types
This commit is contained in:
Nathan Shively-Sanders
2018-12-17 14:49:50 -08:00
committed by GitHub
6 changed files with 167 additions and 0 deletions
+13
View File
@@ -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;
}
}
+85
View File
@@ -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
+40
View File
@@ -0,0 +1,40 @@
// Type definitions for d 1.0
// Project: https://github.com/medikoo/d#readme
// Definitions by: BendingBender <https://github.com/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';
}
+3
View File
@@ -0,0 +1,3 @@
export = lazy;
declare function lazy(obj: { [key: string]: PropertyDescriptor }): PropertyDescriptorMap;
+25
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }