Add types for package react-combine-reducers (#43749)

* Add types for react-combine-reducer

* Run prettier on react-combine-reducers

* Fix linter issues
This commit is contained in:
Raphaël L 2020-04-11 04:42:19 +02:00 committed by GitHub
parent 206382727b
commit 368934537f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 270 additions and 0 deletions

13
types/react-combine-reducers/index.d.ts vendored Normal file
View File

@ -0,0 +1,13 @@
// Type definitions for react-combine-reducers 1.0
// Project: https://github.com/ankita1010/react-combine-reducers#readme
// Definitions by: Raphaël Léger <https://github.com/raphael-leger>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 3.1
import { Reducer, ReducerState } from 'react';
declare function combineReducers<R extends Reducer<any, any>>(
reducers: {
[K in keyof ReducerState<R>]: [Reducer<ReducerState<R>[K], any>, ReducerState<R>[K]];
},
): [R, ReducerState<R>];
export = combineReducers;

View File

@ -0,0 +1,232 @@
import combineReducers from 'react-combine-reducers';
interface SimpleAction {
type: string;
payload: any;
}
interface GreeniesState {
fruit: 'apple' | 'pear' | 'kiwi';
vegetable: 'carrot' | 'eggplant';
}
interface ProteinsState {
similiCarne: 'tofu' | 'seitan';
vegetable: 'bean' | 'artichoke';
}
interface DrinkState {
beverage: 'water' | 'juice';
numberOfGlasses: number;
}
interface DessertState {
cake: 'chocolate' | 'cherry';
drink: 'tea' | 'coffee';
}
interface FullMealState {
proteins: ProteinsState;
greenies: GreeniesState;
dessert: DessertState;
drink: DrinkState;
}
type FullMealReducer = (state: FullMealState, action: SimpleAction) => FullMealState;
const proteinsReducer = (proteinsState: ProteinsState, action: SimpleAction) => {
switch (action) {
default:
return proteinsState;
}
};
const drinkReducer = (drinkState: DrinkState, action: SimpleAction) => {
switch (action) {
default:
return drinkState;
}
};
const dessertReducer = (dessertState: DessertState, action: SimpleAction) => {
switch (action) {
default:
return dessertState;
}
};
const greeniesReducer = (greeniesState: GreeniesState, action: SimpleAction) => {
switch (action) {
default:
return greeniesState;
}
};
// $ExpectError
combineReducers('');
// $ExpectError
combineReducers<FullMealReducer>({});
// $ExpectError
combineReducers<FullMealReducer>();
combineReducers<FullMealReducer>({
// $ExpectError
books: ['The Book Thief'],
});
combineReducers<FullMealReducer>({
// $ExpectError
proteins: [100],
});
combineReducers<FullMealReducer>({
// $ExpectError
proteins: [3, proteinsReducer],
});
combineReducers<FullMealReducer>({
// $ExpectError
proteins: [proteinsReducer, 3],
});
// $ExpectError
combineReducers<FullMealReducer>({
proteins: [
proteinsReducer,
{
similiCarne: 'tofu',
vegetable: 'artichoke',
},
],
});
combineReducers<FullMealReducer>({
proteins: [
proteinsReducer,
{
similiCarne: 'tofu',
vegetable: 'artichoke',
},
],
greenies: [
// $ExpectError
proteinsReducer,
{
fruit: 'kiwi',
vegetable: 'eggplant',
},
],
drink: [
// $ExpectError
proteinsReducer,
{
beverage: 'juice',
numberOfGlasses: 2,
},
],
dessert: [
// $ExpectError
proteinsReducer,
{
cake: 'chocolate',
drink: 'tea',
},
],
});
const [fullMealReducer, initialMeal] = combineReducers<FullMealReducer>({
proteins: [
proteinsReducer,
{
similiCarne: 'tofu',
vegetable: 'artichoke',
},
],
greenies: [
greeniesReducer,
{
fruit: 'kiwi',
vegetable: 'eggplant',
},
],
drink: [
drinkReducer,
{
beverage: 'juice',
numberOfGlasses: 2,
},
],
dessert: [
dessertReducer,
{
cake: 'chocolate',
drink: 'tea',
},
],
});
initialMeal.dessert;
initialMeal.drink;
initialMeal.proteins;
initialMeal.greenies;
// $ExpectError
initialMeal.lamps;
// $ExpectError
fullMealReducer(3, {});
// $ExpectError
fullMealReducer({}, {});
fullMealReducer(
{
proteins: {
similiCarne: 'tofu',
vegetable: 'artichoke',
},
greenies: {
fruit: 'kiwi',
vegetable: 'eggplant',
},
drink: {
beverage: 'juice',
numberOfGlasses: 2,
},
dessert: {
cake: 'chocolate',
drink: 'tea',
},
},
// $ExpectError
67,
);
fullMealReducer(
{
proteins: {
similiCarne: 'tofu',
vegetable: 'artichoke',
},
greenies: {
fruit: 'kiwi',
vegetable: 'eggplant',
},
drink: {
beverage: 'juice',
numberOfGlasses: 2,
},
dessert: {
cake: 'chocolate',
drink: 'tea',
},
},
{
type: 'eat',
payload: 'everything',
},
);
export { fullMealReducer, initialMeal };

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"esModuleInterop": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"react-combine-reducers-tests.ts"
]
}

View File

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