mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-31 19:12:20 +08:00
Merge pull request #19103 from BendingBender/depd
[depd] improve typings, enable strict null checks and linting
This commit is contained in:
@@ -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 = <ITestObject>{ 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 = <TestDeprecatedFunction> {};
|
||||
|
||||
var obj2 = <ITestDeprecatedFunction>{};
|
||||
|
||||
// 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();
|
||||
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
|
||||
});
|
||||
|
||||
45
types/depd/index.d.ts
vendored
45
types/depd/index.d.ts
vendored
@@ -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 <https://github.com/danny8002>
|
||||
// BendingBender <https://github.com/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;
|
||||
}
|
||||
/// <reference types="node" />
|
||||
|
||||
export = depd;
|
||||
|
||||
declare function depd(namespace: string): depd.Deprecate;
|
||||
|
||||
declare namespace depd {
|
||||
interface Deprecate {
|
||||
(message: string): void;
|
||||
// tslint:disable-next-line ban-types
|
||||
function<T extends Function>(fn: T, message?: string): T;
|
||||
property<T extends object>(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[];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
@@ -20,4 +20,4 @@
|
||||
"index.d.ts",
|
||||
"depd-tests.ts"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
1
types/depd/tslint.json
Normal file
1
types/depd/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user