diff --git a/types/react-combine-reducers/index.d.ts b/types/react-combine-reducers/index.d.ts new file mode 100644 index 0000000000..b26df62cc4 --- /dev/null +++ b/types/react-combine-reducers/index.d.ts @@ -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 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// Minimum TypeScript Version: 3.1 +import { Reducer, ReducerState } from 'react'; + +declare function combineReducers>( + reducers: { + [K in keyof ReducerState]: [Reducer[K], any>, ReducerState[K]]; + }, +): [R, ReducerState]; +export = combineReducers; diff --git a/types/react-combine-reducers/react-combine-reducers-tests.ts b/types/react-combine-reducers/react-combine-reducers-tests.ts new file mode 100644 index 0000000000..017b2d7a8c --- /dev/null +++ b/types/react-combine-reducers/react-combine-reducers-tests.ts @@ -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({}); + +// $ExpectError +combineReducers(); + +combineReducers({ + // $ExpectError + books: ['The Book Thief'], +}); + +combineReducers({ + // $ExpectError + proteins: [100], +}); + +combineReducers({ + // $ExpectError + proteins: [3, proteinsReducer], +}); + +combineReducers({ + // $ExpectError + proteins: [proteinsReducer, 3], +}); + +// $ExpectError +combineReducers({ + proteins: [ + proteinsReducer, + { + similiCarne: 'tofu', + vegetable: 'artichoke', + }, + ], +}); + +combineReducers({ + 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({ + 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 }; diff --git a/types/react-combine-reducers/tsconfig.json b/types/react-combine-reducers/tsconfig.json new file mode 100644 index 0000000000..8415c64e1f --- /dev/null +++ b/types/react-combine-reducers/tsconfig.json @@ -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" + ] +} diff --git a/types/react-combine-reducers/tslint.json b/types/react-combine-reducers/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-combine-reducers/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }