[once] upgrade to 1.4, improve typings, enable strict null checks and linting

This commit is contained in:
Dimitri Benin
2017-08-22 09:14:52 +02:00
parent 49beb4b010
commit eb9ef4cc2d
4 changed files with 90 additions and 24 deletions

53
types/once/index.d.ts vendored
View File

@@ -1,23 +1,48 @@
// Type definitions for once v1.3.3
// Type definitions for once 1.4
// Project: https://github.com/isaacs/once
// Definitions by: Denis Sokolov <https://github.com/denis-sokolov>
// BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
interface SimpleFunction<Result> {
(...args: any[]): Result;
export = once;
declare const once: Once;
interface Once extends once.OnceFn {
proto(): void;
strict: once.OnceFn;
}
interface OnceFunction<Result> extends SimpleFunction<Result> {
called: boolean;
value: Result;
declare namespace once {
interface OnceFn {
<R>(f: () => R): (() => R) & FnProps<R>;
<T1, R>(f: (t1: T1) => R): ((t1: T1) => R) & FnProps<R>;
<T1, T2, R>(f: (t1: T1, t2: T2) => R): ((t1: T1, t2: T2) => R) & FnProps<R>;
<T1, T2, T3, R>(f: (t1: T1, t2: T2, t3: T3) => R): ((t1: T1, t2: T2, t3: T3) => R) & FnProps<R>;
<T1, T2, T3, T4, R>(f: (t1: T1, t2: T2, t3: T3, t4: T4) => R): ((t1: T1, t2: T2, t3: T3, t4: T4) => R) & FnProps<R>;
<T1, T2, T3, T4, T5, R>(f: (t1: T1, t2: T2, t3: T3, t4: T4, t5: T5) => R): ((t1: T1, t2: T2, t3: T3, t4: T4, t5: T5) => R) & FnProps<R>;
<T1, T2, T3, T4, T5, T6, R>(f: (t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6) => R):
((t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6) => R) & FnProps<R>;
<T1, T2, T3, T4, T5, T6, T7, R>(f: (t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7) => R):
((t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7) => R) & FnProps<R>;
<T1, T2, T3, T4, T5, T6, T7, T8, R>(f: (t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8) => R):
((t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8) => R) & FnProps<R>;
<T1, T2, T3, T4, T5, T6, T7, T8, T9, R>(f: (t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8, t9: T9) => R):
((t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8, t9: T9) => R) & FnProps<R>;
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R>(f: (t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8, t9: T9, t10: T10) => R):
((t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8, t9: T9, t10: T10) => R) & FnProps<R>;
<R>(f: (...args: any[]) => R): ((...args: any[]) => R) & FnProps<R>;
}
interface FnProps<R> {
called: boolean;
value: R | undefined;
}
}
interface Once {
<Result>(f: SimpleFunction<Result>): OnceFunction<Result>;
proto: Function;
}
declare module "once" {
var once: Once;
export default once;
declare global {
interface Function {
// tslint:disable-next-line ban-types
once(): Function & once.FnProps<any>;
}
}

View File

@@ -1,13 +1,53 @@
import once = require('once');
import once from "once";
// $ExpectType (() => number) & FnProps<number>
once(() => 3);
once(() => 3)();
let s = once(() => ({foo: 1}))();
s.foo;
// $ExpectType ((t1: string) => number) & FnProps<number>
once((t1: string) => 3);
// $ExpectType ((t1: string, t2: null) => number) & FnProps<number>
once((t1: string, t2: null) => 3);
// $ExpectType ((t1: string, t2: null, t3: string) => number) & FnProps<number>
once((t1: string, t2: null, t3: string) => 3);
// $ExpectType ((t1: string, t2: null, t3: string, t4: null) => number) & FnProps<number>
once((t1: string, t2: null, t3: string, t4: null) => 3);
// $ExpectType ((t1: string, t2: null, t3: string, t4: null, t5: number) => number) & FnProps<number>
once((t1: string, t2: null, t3: string, t4: null, t5: number) => 3);
// $ExpectType ((t1: string, t2: null, t3: string, t4: null, t5: number, t6: null) => number) & FnProps<number>
once((t1: string, t2: null, t3: string, t4: null, t5: number, t6: null) => 3);
// $ExpectType ((t1: string, t2: null, t3: string, t4: null, t5: number, t6: null, t7: string) => number) & FnProps<number>
once((t1: string, t2: null, t3: string, t4: null, t5: number, t6: null, t7: string) => 3);
// $ExpectType ((t1: string, t2: null, t3: string, t4: null, t5: number, t6: null, t7: string, t8: null) => number) & FnProps<number>
once((t1: string, t2: null, t3: string, t4: null, t5: number, t6: null, t7: string, t8: null) => 3);
// $ExpectType ((t1: string, t2: null, t3: string, t4: null, t5: number, t6: null, t7: string, t8: null, t9: string) => number) & FnProps<number>
once((t1: string, t2: null, t3: string, t4: null, t5: number, t6: null, t7: string, t8: null, t9: string) => 3);
// $ExpectType ((t1: string, t2: null, t3: string, t4: null, t5: number, t6: null, t7: string, t8: null, t9: string, t10: null) => number) & FnProps<number>
once((t1: string, t2: null, t3: string, t4: null, t5: number, t6: null, t7: string, t8: null, t9: string, t10: null) => 3);
// $ExpectType ((...args: any[]) => number) & FnProps<number>
once((t1: string, t2: null, t3: string, t4: null, t5: number, t6: null, t7: string, t8: null, t9: string, t10: null, t11: string) => 3);
once(() => 3)(); // $ExpectType number
once(() => ({foo: 1}))(); // $ExpectType { foo: number; }
once(() => 3).called; // $ExpectType boolean
once(() => ({foo: 1})).value; // $ExpectType { foo: number; } | undefined
once.proto();
once(() => 3).called && true;
once(() => ({foo: 1})).value.foo;
once.strict(() => 3); // $ExpectType (() => number) & FnProps<number>
Function.prototype.once; // ExpectType () => Function & once.FnProps<any>;
function fn() {
return 3;
}
fn.once(); // $ExpectType Function & FnProps<any>
function greet(name: string | null, cb: (greeting: string) => void) {
if (!name) cb('Hello anonymous');
cb('Hello ' + name);
}
function log(msg: any) {
}
// this will print 'Hello anonymous' but the logical error will be missed
greet(null, once(log));
// once.strict will print 'Hello anonymous' and throw an error when the callback will be called the second time
greet(null, once.strict(log));

View File

@@ -6,7 +6,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
@@ -19,4 +19,4 @@
"index.d.ts",
"once-tests.ts"
]
}
}

1
types/once/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }