Comments (7)
I figured it out and thats work :)
type Types = "Success" | "Error" | "Done"
type SnakeToCamelCase<S extends string> = S extends `${infer T}_${infer U}` ? `${Lowercase<T>}${Capitalize<Lowercase<SnakeToCamelCase<U>>>}` : S
type Output<T extends string> = {
[key in `${SnakeToCamelCase<T>}${Types}`]: AnyAction
}
I've attached code, maybe somebody also need it. Thank you very much again for help :)
from deox.
Hi, The problem arise from conversion of name
to actionName
. As you see the type of name
and actionName
is string not a literal string type. You need something like this:
function createRequestAction<T extends string>(name: T) {
const actionName: F<T> = ...
return {
[`${actionName}Success`]: createActionCreator(`${name}_SUCCESS`),
[`${actionName}Error`]: createActionCreator(`${name}_ERROR`),
[`${actionName}Done`]: createActionCreator(`${name}_DONE`),
}
}
The F
type level function converts input literal type to your desired output literal type.
from deox.
Hi, thank you for your suggestions, I've tried to type this but it still doesn't work and I don't know how to fix this
type ActionStringType<S extends string, D extends string> = string extends S
? ""
: S extends ""
? ""
: S extends `${infer T}${D}${infer U}`
? `${Lowercase<T>}${Capitalize<ActionStringType<U, D>>}`
: Capitalize<Lowercase<S>>
const createRequestAction = <T extends string>(name: T) => {
const actionName = name
.split("_")
.map((item, i) => {
if (i === 0) {
return item.toLowerCase()
}
return item.charAt(0).toUpperCase() + item.slice(1).toLowerCase()
})
.join("") as ActionStringType<T, "_">
return {
[`${actionName}Success`]: createActionCreator(`${name}_SUCCESS`),
[`${actionName}Error`]: createActionCreator(`${name}_ERROR`),
[`${actionName}Done`]: createActionCreator(`${name}_DONE`),
}
}
I've made a ActionStringType
and for example:
type S1 = ActionStringType<"SET_AS_FAVOURITE">
// returns "setAsFavourite" which is good
So this type is working fine but TS is still not seeing the names (keys of object) of action creators.
from deox.
And for example now something like ...createRequestAction("SET_AS_FAVOURITE")
returns:
const createRequestAction: <"SET_AS_FAVOURITE">(name: "SET_AS_FAVOURITE") => {
[x: string]: ExactActionCreator<"SET_AS_FAVOURITE_SUCCESS", () => {
type: "SET_AS_FAVOURITE_SUCCESS";
}> | ExactActionCreator<"SET_AS_FAVOURITE_ERROR", () => {
...;
}> | ExactActionCreator<...>;
}
I want to change this [x: string]
to something like S1 type returns
from deox.
Unfortunately, TS doesn't have a nice sync between type-level and term-level, which will becomes its biggest regret soon. :)
So in simple terms, the compiler is dumb and the good news is that you as a human being are smart. Just shut the compiler mouth up with any
type and calculate the output type on your own bare foot. (SPOILER ALERT! Maintenance burden; but it works!)
function f<a extends string>(a: a): Output<a> {
// ...
return { ... } as any
}
from deox.
Hmm, i think it won't work because I can't use ActionStringType
type as a value in object, I mean can't do something like this:
type Output<T> = {
[key: `${ActionStringType}Success`<T, "_"> ]: AnyAction
[key: `${ActionStringType}Error`<T, "_"> ]: AnyAction
[key: `${ActionStringType}Done`<T, "_"> ]: AnyAction
}
Please attach more code if you have some idea, i can test it ;)
from deox.
type Types = "Success" | "Error" | "Done"
type Output<a> = {
[key in `${SnakeCaseToCamelCase<a>}${Types}`]: AnyAction
}
from deox.
Related Issues (20)
- handleAction allows you to return a state object with any additional properties HOT 3
- 2.1.2 breaking change on ActionCreator type HOT 4
- The automated release is failing 🚨
- The automated release is failing 🚨 HOT 1
- ofType does no longer automatically infer action type from general source in pipe HOT 6
- createAction with only a type parameter results in function, which allows any arguments HOT 5
- Failed to resolve 'rxjs/operators' HOT 2
- Explain or remove `resolve => x => resolve(x)` pattern HOT 1
- Create polymorphic (generic) action creator HOT 2
- ESLint explicit-function-return-type rule error HOT 4
- Bundle size notes HOT 5
- Add "default" section in createReducer() HOT 4
- The automated release is failing 🚨
- Why was Deox deprecated? HOT 1
- The automated release is failing 🚨 HOT 1
- Prototype pollution in immer dependency HOT 2
- The automated release is failing 🚨
- Add Payload type utility HOT 1
- Looking for a maintainer...
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deox.