blakeembrey / change-case

Convert strings between camelCase, PascalCase, Capital Case, snake_case and more
MIT License
2.21k stars 92 forks source link

Type resulting object properties correctly when using change-case/keys #341

Open dnjstrom opened 2 months ago

dnjstrom commented 2 months ago

When using change-case/keys I would love for the resulting object to be typed correctly without having to do an explicit cast. Currently all transformations produce unknown.

import { camelCase } from "change-case/keys"
import type { CamelCasedProperties } from 'type-fest';

const snake_cased = { foo_bar: 1 }

// Preferably the cast could be left off on the line below
const camelCased = camelCase(snake_cased) as CamelCasedProperties<typeof snake_cased>

console.log(camelCased.fooBar) // => 1
hxtruong6 commented 1 month ago

I faced to the same issue when try to use the examples image

blakeembrey commented 1 month ago

@hxtruong6 that is unrelated to the original report so I’ll be hiding the comment. Try searching closed issues instead, it’s because your typescript configuration is incorrect for ESM support.

blakeembrey commented 1 month ago

I think I closed a past issue on the complexity on typing these, but I think it could be good to have available. I would accept a PR if anyone wants to attempt this.

Previous issue: https://github.com/blakeembrey/change-case/issues/294.

hxtruong6 commented 1 month ago

@hxtruong6 that is unrelated to the original report so I’ll be hiding the comment. Try searching closed issues instead, it’s because your typescript configuration is incorrect for ESM support.

  "module": "NodeNext",

Yes. You are right when I change the tsconfig.json with other module. Thank you.