mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-03 22:37:03 +00:00
Merge pull request #32979 from karol-majewski/use-persisted-state
[use-persisted-state] Add new definition
This commit is contained in:
Vendored
+11
@@ -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;
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user