Comments (10)
I could be wrong, but I think #54 broke the expected return type definition. It made it so that Typescript thinks that camelcaseKeys
will return the exact same input it gets as the output ... which isn't the case.
const obj = { snake_case: "yuk" };
const camelized = camelcaseKeys(obj);
// Typescript thinks camelized looks like:
// { snake_case: "yuk" }
// but it actually looks like:
// { snakeCase: "yuk" }
I don't know if there's a way to camelcase keys in interfaces/types, but perhaps a library like this simply needs to return a generic Record<string, any>
or only pass on the values of the object, instead of the exact input it gets.
from camelcase-keys.
This might be worth looking into microsoft/TypeScript#12754
from camelcase-keys.
There's a linked PR right above your comment.
from camelcase-keys.
@OmgImAlexis Indeed! Do you see a particular solution in there? I'm admittedly pretty green when it comes to TS, so I'm having trouble deciphering some of the proposed "hacks" (considering there's no official TS idiom for augmented mapped types)
from camelcase-keys.
@OmgImAlexis I'm also not able to follow the suggestions in that discussion thread. Can you point me in the right direction of how those examples might apply here? It seems like @mattrothenberg's proposed solution of defining two types (one camel cased, one snake case) isn't ideal but it's better than my current workaround of casting to an unknown and then casting to my target type.
@sindresorhus do you have any thoughts on how to fix the issue introduced by #54?
from camelcase-keys.
I don't have any good solution for this. I think we should just revert #54.
// @jmca
from camelcase-keys.
Here are some immediate things you can do. There are more variations, but either way with current TS (unless something new in TS was introduced that I'm not aware of), you need to cast the object to the targeted type.
const someObj = { 'foo-bar': true }
{
type CamelType = { fooBar: boolean }
const cc = camelcaseKeys<CamelType>(someObj as any)
cc.fooBar
cc['foo-bar'] // No complaint, but not typed
}
// or
{
type CamelType = { fooBar: boolean }
type CombinedCamelType = Partial<CamelType & typeof someObj>
const cc = camelcaseKeys<CombinedCamelType>(someObj)
cc.fooBar
cc['foo-bar'] // Typed!
}
// or
{
const cc = camelcaseKeys(someObj as any)
cc.justPlainProps
}
// or
const cc = '🤪'
from camelcase-keys.
@sindresorhus any chance this is now possible with https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html
Some good examples in the OP microsoft/TypeScript#40336
from camelcase-keys.
Not sure how I missed that.
from camelcase-keys.
If anyone wants to work on this, see the previous attempt and feedback in #69.
from camelcase-keys.
Related Issues (20)
- Trying to use this lib with Nest.js, and getting import error HOT 1
- Unexpected token name «_000», expected punc «,» in file node_modules/camelcase-keys/index.js at 25:16 HOT 1
- Error: Failed to load function definition from source HOT 1
- iOS archive failed - phasescriptexecution failed with a nonzero exit code HOT 1
- This module's config insufficiently robust for use in Typescript HOT 3
- Failed import camelcase-keys HOT 2
- [TypeScript] If the `deep: true` option is set, even the object's properties will be converted.
- type mismatch with reality HOT 1
- Type 'Interface' does not satisfy the constraint 'readonly any[] | ObjectOptional'. HOT 6
- Using function with inferred type does not resolve correct return type defaults HOT 1
- Version 9 does not recursively camelCase keys in nested objects when using union with null HOT 2
- [Safari] Error on page load 12.1 HOT 1
- Documentation doesn't match the types? HOT 1
- Handle underscore as the first character HOT 1
- [TS] Result and result type are different HOT 1
- Why don't you provide cjs build? HOT 1
- `Error [ERR_REQUIRE_ESM]`: Must use import to load ES Module: `ROOT\node_modules\camelcase-keys\index.js` HOT 7
- An issue of Cannot use import statement outside a module HOT 1
- FYI linter errors importing module HOT 1
- fix: typing errors with typescript 4.8 HOT 3
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 camelcase-keys.