[react-cache] Add typings for react-cache (#30446)

* Generate the standard tsconfig/tslint types

* Add the core types for react-cache

* Add test code for react-cache types

* Add strictFunctionTypes to tsconfig

* Resolve linting warnings

* Use global PromiseLike type
This commit is contained in:
Spencer Miskoviak
2018-11-18 19:20:07 -08:00
committed by Pranav Senthilnathan
parent 8146643dd5
commit cf9e4f7842
4 changed files with 63 additions and 0 deletions

14
types/react-cache/index.d.ts vendored Normal file
View File

@@ -0,0 +1,14 @@
// Type definitions for react-cache 2.0
// Project: https://github.com/facebook/react/tree/master/packages/react-cache
// Definitions by: Spencer Miskoviak <https://github.com/skovy>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export interface Resource<Input, Value> {
read(key: Input): Value;
preload(key: Input): void;
}
export function unstable_createResource<Input, Value>(
fetch: (input: Input) => PromiseLike<Value>,
maybeHashInput?: (input: Input) => string | number
): Resource<Input, Value>;

View File

@@ -0,0 +1,25 @@
import { unstable_createResource } from "react-cache";
const fetchName = (name: string) =>
new Promise<string>((resolve, reject) => {
if (name === "Bob") {
resolve(name);
} else {
reject(`Invalid name: ${name}`);
}
});
const nameResource = unstable_createResource(fetchName);
nameResource.read("Bob");
const fetchUser = ({ name }: { name: string }) =>
new Promise<string>((resolve, reject) => {
if (name === "Jill") {
resolve(name);
} else {
reject(`Invalid name: ${name}`);
}
});
const userResource = unstable_createResource(fetchUser, ({ name }) => name);
userResource.read({ name: "Jill" });

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",
"react-cache-tests.ts"
]
}

View File

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