diff --git a/types/depd/depd-tests.ts b/types/depd/depd-tests.ts index 62a4470f15..f201cb504a 100644 --- a/types/depd/depd-tests.ts +++ b/types/depd/depd-tests.ts @@ -1,52 +1,41 @@ import depd = require('depd'); -var deprecate = depd("depd-tests"); +const deprecate = depd("depd-tests"); -function assert(condition: boolean, message: string): void { - if (!condition) { - throw new Error(message); - } -} +deprecate('message'); -function testDepdMessage(...args: string[]): boolean { - if (arguments.length < 1) { - deprecate('testDepdMessage argument.lenth<1'); - return true; - } else { - console.log('normal logic'); - return false; - } -} - -assert(testDepdMessage() === true, "Deprecated code must be triggered!"); -assert(testDepdMessage('a') === false, "Deprecated code must be triggered!"); - -interface ITestObject { - p1: string; - p2: string; -} - -var obj = { p1: 'deprecated property', p2: 'normal property' }; +const obj = { p1: 'deprecated property', p2: 'normal property' }; deprecate.property(obj, 'p1', 'property [p1] is deprecated!'); +deprecate.property(obj, 'p3', 'property [p3] is deprecated!'); // $ExpectError -console.log(obj.p1); - -interface ITestDeprecatedFunction { - func1?: Function; - func2?: Function; +interface TestDeprecatedFunction { + func1?(): void; + func2?(arg: string): boolean; } +const obj2 = {}; -var obj2 = {}; - -// message automatically derived from function name -obj2.func1 = deprecate.function(function func1() { +obj2.func1 = deprecate.function(() => { console.log('all calls to [func1] are deprecated '); }); -// specific message -obj2.func2 = deprecate.function(function () { +// $ExpectError +obj2.func2 = deprecate.function(() => { console.log('all calls to [func2] are deprecated '); }, 'func2'); +obj2.func2 = deprecate.function((arg: string) => { + console.log('all calls to [func2] are deprecated '); + return true; +}, 'func2'); + obj2.func1(); -obj2.func2(); \ No newline at end of file +obj2.func2(''); + +process.on('deprecation', error => { + const err: depd.DeprecationError = error; + error; // $ExpectType DeprecationError + + err.name; // $ExpectType "DeprecationError" + err.namespace; // $ExpectType string + err.stack; // $ExpectType string +}); diff --git a/types/depd/index.d.ts b/types/depd/index.d.ts index 0db1d877f6..fa1c3a71fe 100644 --- a/types/depd/index.d.ts +++ b/types/depd/index.d.ts @@ -1,16 +1,41 @@ -// Type definitions for depd 1.1.0 +// Type definitions for depd 1.1 // Project: https://github.com/dougwilson/nodejs-depd // Definitions by: Zhiyuan Wang +// BendingBender // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 - - -declare function depd(namespace: string): Deprecate; - -interface Deprecate { - (message: string): void; - function(fn: Function, message?: string): Function; - property(obj: Object, prop: string, message: string): void; -} +/// export = depd; + +declare function depd(namespace: string): depd.Deprecate; + +declare namespace depd { + interface Deprecate { + (message: string): void; + // tslint:disable-next-line ban-types + function(fn: T, message?: string): T; + property(obj: T, prop: keyof T, message: string): void; + } + + interface DeprecationError extends Error { + readonly name: 'DeprecationError'; + namespace: string; + stack: string; + } +} + +declare global { + namespace NodeJS { + interface Process { + addListener(event: 'deprecation', listener: (deprecationError: depd.DeprecationError) => void): this; + emit(event: 'deprecation', code: depd.DeprecationError): boolean; + on(event: 'deprecation', listener: (deprecationError: depd.DeprecationError) => void): this; + once(event: 'deprecation', listener: (deprecationError: depd.DeprecationError) => void): this; + prependListener(event: 'deprecation', listener: (deprecationError: depd.DeprecationError) => void): this; + prependOnceListener(event: 'deprecation', listener: (deprecationError: depd.DeprecationError) => void): this; + listeners(event: 'deprecation'): depd.DeprecationError[]; + } + } +} diff --git a/types/depd/tsconfig.json b/types/depd/tsconfig.json index 14bec0a373..23aa25d9e0 100644 --- a/types/depd/tsconfig.json +++ b/types/depd/tsconfig.json @@ -7,7 +7,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -20,4 +20,4 @@ "index.d.ts", "depd-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/depd/tslint.json b/types/depd/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/depd/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }