huan / ducks Goto Github PK
View Code? Open in Web Editor NEWReducer Bundles Manager for Redux, Implemented the Ducks Modular Proposal
Home Page: https://paka.dev/npm/ducks
License: Apache License 2.0
Reducer Bundles Manager for Redux, Implemented the Ducks Modular Proposal
Home Page: https://paka.dev/npm/ducks
License: Apache License 2.0
When we define the redux action types in ducks, we will store them in a types.ts
file:
const TEST = 'module/TEST'
export const TEST
And use import * as types from './types'
in other TS module to import all the types.
In this way, the TEST
can keep the string literal type as module/TEST
, which is necessary for future usage.
export default { TEST }
In this way, the typeof TEST
will become string
, which will lose its string literal type.
If it lists its string literal type, then it seems more likely will cause the problem in future usage.
Using string constants as action type property:
please make sure to use simple string literal assignment with const. This limitation is coming from the type-system, because all the dynamic string operations (e.g. string concatenation, template strings and also object used as a map) will widen the literal type to its super-type, string. As a result this will break contextual typing for action object in reducer cases.
Minimum reproducible code snip:
import { Reducer, AnyAction } from 'redux'
type MyAction = {
payload: number,
type: 'TAP',
}
const f: Reducer<any, MyAction> = (s: any, ..._: any[]) => s
const r: Reducer<any, AnyAction> = f
// r is NOT ok.
void r
The error message of r
is:
Type 'Reducer<any, MyAction>' is not assignable to type 'Reducer<any, AnyAction>'.
Types of parameters 'action' and 'action' are incompatible.
Type 'AnyAction' is not assignable to type 'MyAction'.ts(2322)
However,
const a1: MyAction = {
payload: 42,
type: 'TAP',
}
const a2: AnyAction = a1
// a2 is OK
void a2
To be fixed.
Super-type of T is something like <U extends T>
microsoft/TypeScript#16985 (comment)When running under ES Modules mode:
$ node --no-warnings --loader=ts-node/esm ./src/ducks/ducks.spec.ts
/Users/huan/git/huan/ducks/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:379
const err = new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base));
^
CustomError: ERR_UNSUPPORTED_DIR_IMPORT /Users/huan/git/huan/ducks/node_modules/redux-saga/effects /Users/huan/git/huan/ducks/examples/ping-pong/sagas.ts
ducks/examples/ping-pong/sagas.spec.ts
Lines 22 to 25 in d96f35b
To disable saga temporarily and will investigate in the future.
to let other developers inspect this convenience module and collect feedbacks.
Links:
TODO:
When running with ES Module mode, we have to:
import * as MOCK_STORE 'redux-mock-store'
const createMockStore = (MOCK_STORE.default as any).default as typeof MOCK_STORE.default
The above code will violate the Typing system (must use as any
), but it works for the final code.
To be investigated.
"redux-mock-store": "^1.5.4"
"@types/redux-mock-store": "^1.0.3"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.