GithubHelp home page GithubHelp logo

Comments (10)

trymbill avatar trymbill commented on May 21, 2024 5

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.

OmgImAlexis avatar OmgImAlexis commented on May 21, 2024 1

This might be worth looking into microsoft/TypeScript#12754

from camelcase-keys.

sindresorhus avatar sindresorhus commented on May 21, 2024 1

There's a linked PR right above your comment.

from camelcase-keys.

mattrothenberg avatar mattrothenberg commented on May 21, 2024

@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.

pfarnach avatar pfarnach commented on May 21, 2024

@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.

sindresorhus avatar sindresorhus commented on May 21, 2024

I don't have any good solution for this. I think we should just revert #54.

// @jmca

from camelcase-keys.

jmca avatar jmca commented on May 21, 2024

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.

OmgImAlexis avatar OmgImAlexis commented on May 21, 2024

@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.

OmgImAlexis avatar OmgImAlexis commented on May 21, 2024

Not sure how I missed that.

from camelcase-keys.

sindresorhus avatar sindresorhus commented on May 21, 2024

If anyone wants to work on this, see the previous attempt and feedback in #69.

from camelcase-keys.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.