Add typings for the promise-hash library, v1.3 (#40331)

* Added typings for this promise-hash library

* pull out common type for the promise hash argument

* make recommended simplifications, expand test
This commit is contained in:
Michael Shafir 2019-11-14 18:31:23 -05:00 committed by Pranav Senthilnathan
parent b692892009
commit 6061d28041
4 changed files with 63 additions and 0 deletions

16
types/promise-hash/index.d.ts vendored Normal file
View File

@ -0,0 +1,16 @@
// Type definitions for promise-hash 1.3
// Project: https://github.com/mtimofiiv/promise-hash
// Definitions by: Michael Shafir <https://github.com/mshafir>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export = hash;
type PromiseHash = <T>(promiseHash: { [P in keyof T]: PromiseLike<T[P]> | T[P] }) => Promise<T>;
declare global {
interface PromiseConstructor {
hash: PromiseHash;
}
}
declare const hash: PromiseHash;

View File

@ -0,0 +1,23 @@
import hash = require('promise-hash');
const result = Promise.hash({
a: 5,
b: Promise.resolve("test"),
c: Promise.hash({
d: Promise.resolve(true),
e: Promise.resolve("again")
})
});
type expectedType = Promise<{ a: number; b: string; c: { d: boolean; e: string; } }>;
type resultIsExpected = typeof result extends expectedType ? true : false; // $ExpectType true
type expectedIsResult = expectedType extends typeof result ? true : false; // $ExpectType true
const dResult = result.then(x => x.c.d);
dResult; // $ExpectType Promise<boolean>
const result2 = hash({
test: Promise.resolve("hello world")
});
result2; // $ExpectType Promise<{ test: string; }>

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"promise-hash-tests.ts"
]
}

View File

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