Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: more stricter inference for capitalCase #37

Open
devarsh-mavani-19 opened this issue Aug 30, 2024 · 1 comment
Open

Proposal: more stricter inference for capitalCase #37

devarsh-mavani-19 opened this issue Aug 30, 2024 · 1 comment

Comments

@devarsh-mavani-19
Copy link

devarsh-mavani-19 commented Aug 30, 2024

Hey @mesqueeb

I have been an active user of this library across many projects. One problem faced is that the library doesn't retain the literal types. It converts everything to string. For example

type Fruit = "orange" | "banana" | "apple"
const myFruit: Fruit ="orange";
const capitalCaseFruit = capitalCase(myFruit); // type of capitalCaseFruit = string instead of "Orange" | "Banana" | "Apple";

Why this would be useful?

I have had situations where I want to use the value to index an object. and if the value is string then I have to typecast it explicitly.
Example from above

const obj = {
    priceOrange: 10,
    priceBanana: 10,
    priceApple: 10,
}

obj[`price${capitalCaseFruit}`] = 20; // this is not typescript friendly.

of course simple thing to do is typecast it. But it would be great to have inbuilt type inference.

I am willing to contribute to this feature if you guys are open to outside contributions. and eventually, we can add this support to all case types. I am not sure how difficult it would be but I all ears to suggestions. maybe we can have a separate functions for more stricter types? please let me know what you think about this.

@mesqueeb
Copy link
Owner

I personally only use this with dynamic data, not typescript typed strings.
I believe that if you want to implement correct typescript type inference, you probably need to re-implement the entire JS logic in pure TS somehow. I'm not sure how that could be possible.

PRs welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants