mesqueeb / case-anything

camelCase, kebab-case, PascalCase... a simple integration with nano package size. (SMALL footprint!)
https://github.com/sponsors/mesqueeb
MIT License
99 stars 3 forks source link

Proposal: more stricter inference for capitalCase #37

Open devarsh-mavani-19 opened 2 months ago

devarsh-mavani-19 commented 2 months ago

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 commented 2 months ago

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.