From bf696e60dbb8945661631df8d0b54699f13b2330 Mon Sep 17 00:00:00 2001 From: Karol Majewski Date: Tue, 12 Feb 2019 03:37:55 +0100 Subject: [PATCH] [use-persisted-state] Add new definition --- types/use-persisted-state/index.d.ts | 11 +++++ types/use-persisted-state/tsconfig.json | 24 ++++++++++ types/use-persisted-state/tslint.json | 1 + .../use-persisted-state-tests.ts | 45 +++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 types/use-persisted-state/index.d.ts create mode 100644 types/use-persisted-state/tsconfig.json create mode 100644 types/use-persisted-state/tslint.json create mode 100644 types/use-persisted-state/use-persisted-state-tests.ts diff --git a/types/use-persisted-state/index.d.ts b/types/use-persisted-state/index.d.ts new file mode 100644 index 0000000000..76b0271986 --- /dev/null +++ b/types/use-persisted-state/index.d.ts @@ -0,0 +1,11 @@ +// Type definitions for use-persisted-state 0.3 +// Project: https://github.com/donavon/use-persisted-state#readme +// Definitions by: Karol Majewski +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 +import { useState } from 'react'; + +declare function createPersistedState(key: string, provider?: Pick): typeof useState; + +export as namespace createPersistedState; +export default createPersistedState; diff --git a/types/use-persisted-state/tsconfig.json b/types/use-persisted-state/tsconfig.json new file mode 100644 index 0000000000..e6765dca73 --- /dev/null +++ b/types/use-persisted-state/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "use-persisted-state-tests.ts" + ] +} diff --git a/types/use-persisted-state/tslint.json b/types/use-persisted-state/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/use-persisted-state/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/use-persisted-state/use-persisted-state-tests.ts b/types/use-persisted-state/use-persisted-state-tests.ts new file mode 100644 index 0000000000..74c7a8eee7 --- /dev/null +++ b/types/use-persisted-state/use-persisted-state-tests.ts @@ -0,0 +1,45 @@ +import createPersistedState from 'use-persisted-state'; +import { useState } from 'react'; + +/** + * Only the first argument is required. + */ +createPersistedState('myKey'); + +/** + * The generated hook conforms to the same signature as `React.useState`. + */ +const myHook: typeof useState = createPersistedState('myKey'); + +/** + * Custom storage provider can be passed as the second argument. + */ +createPersistedState('myKey', localStorage); +createPersistedState('myKey', sessionStorage); + +/** + * The provider is similar to the `Storage` interface, + * but cares only about `getItem` and `setItem` methods. + */ +const { getItem, setItem } = localStorage; +createPersistedState('myKey', { getItem, setItem }); + +/** + * At least one argument must be provided. + */ +createPersistedState(); // $ExpectError + +/** + * The `key` argument must be of type `string`. + */ +createPersistedState(1); // $ExpectError +createPersistedState({}); // $ExpectError +createPersistedState(Symbol.iterator); // $ExpectError +createPersistedState(undefined); // $ExpectError + +/** + * The `provider` argument, when passed, must partially implement `Storage`. + */ +createPersistedState('myKey', { }); // $ExpectError +createPersistedState('myKey', { getItem: localStorage.getItem }); // $ExpectError +createPersistedState('myKey', { setItem: localStorage.setItem }); // $ExpectError