Merge pull request #32979 from karol-majewski/use-persisted-state

[use-persisted-state] Add new definition
This commit is contained in:
Mine Starks
2019-02-12 15:22:16 -08:00
committed by GitHub
4 changed files with 81 additions and 0 deletions
+11
View File
@@ -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 <https://github.com/karol-majewski>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
import { useState } from 'react';
declare function createPersistedState(key: string, provider?: Pick<Storage, 'getItem' | 'setItem'>): typeof useState;
export as namespace createPersistedState;
export default createPersistedState;
+24
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }
@@ -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