mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
[use-subscription] Add types (#38324)
This commit is contained in:
parent
adc769c638
commit
717634b5f4
21
types/use-subscription/index.d.ts
vendored
Normal file
21
types/use-subscription/index.d.ts
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
// Type definitions for use-subscription 1.0
|
||||
// Project: https://github.com/facebook/react/
|
||||
// Definitions by: Sebastian Silbermann <https://github.com/eps1lon>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.8
|
||||
|
||||
export type Unsubscribe = () => void;
|
||||
|
||||
export interface Subscription<T> {
|
||||
/**
|
||||
* (Synchronously) returns the current value of our subscription.
|
||||
*/
|
||||
getCurrentValue: () => T;
|
||||
/**
|
||||
* This function is passed an event handler to attach to the subscription.
|
||||
* It must return an unsubscribe function that removes the handler.
|
||||
*/
|
||||
subscribe: (callback: () => void) => Unsubscribe;
|
||||
}
|
||||
|
||||
export function useSubscription<T>(subscription: Subscription<T>): T;
|
||||
24
types/use-subscription/tsconfig.json
Normal file
24
types/use-subscription/tsconfig.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6",
|
||||
"dom"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictFunctionTypes": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"use-subscription-tests.ts"
|
||||
]
|
||||
}
|
||||
1
types/use-subscription/tslint.json
Normal file
1
types/use-subscription/tslint.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
22
types/use-subscription/use-subscription-tests.ts
Normal file
22
types/use-subscription/use-subscription-tests.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { useMemo } from 'react';
|
||||
import { useSubscription, Subscription } from 'use-subscription';
|
||||
|
||||
// https://github.com/facebook/react/tree/d96f478f8a79da3125f6842c16efbc2ae8bcd3bf/packages/use-subscription#subscribing-to-event-dispatchers
|
||||
function EventDispatcherExample({ input }: { input: HTMLInputElement }) {
|
||||
const subscription = useMemo(
|
||||
(): Subscription<string> => ({
|
||||
getCurrentValue: () => input.value,
|
||||
subscribe: callback => {
|
||||
input.addEventListener('change', callback);
|
||||
return () => input.removeEventListener('change', callback);
|
||||
},
|
||||
}),
|
||||
|
||||
[input],
|
||||
);
|
||||
|
||||
// $ExpectType string
|
||||
const value = useSubscription(subscription);
|
||||
|
||||
// Your rendered output goes here ...
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user