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

Define function, which transforms an object, keeping keys #6095

Closed
Strate opened this issue Dec 14, 2015 · 5 comments
Closed

Define function, which transforms an object, keeping keys #6095

Strate opened this issue Dec 14, 2015 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@Strate
Copy link

Strate commented Dec 14, 2015

I need to define a function, which accepts an object of this type:

interface Source<A> {
    [index: string]: A
}

and transforms that object, keeping the keys, but replaces a values:

interface Target<B> {
    [index: string]: B
}

and I also want to keep typechecking for that case. This is JS example:

function transform(source) {
    var result = {}
    Object.keys(source).forEach((key) => {
        result[key] = source[key] + "prefix"
    })
}

var target = transform({
    "key1": 1,
    "key2": 2,
})

// now target has a {"key1": "1prefix", "key2": "2prefix"}

var three = target.key3 // I want to get type error here on compile-time

Any way to do that?

@RyanCavanaugh
Copy link
Member

There isn't a way to represent this in the type system today.

@Strate
Copy link
Author

Strate commented Dec 14, 2015

Any plans to future? Maybe some existing issue?

@saschanaz
Copy link
Contributor

This may be close but the problem here is the value types of key1 and key2 is not string|number but number.

interface SourceOrTarget<T> {
    [index: string]: T
}

function transform<T extends SourceOrTarget<string|number>>(source: T) {
    var result = {} as T;
    Object.keys(source).forEach((key) => {
        result[key] = source[key] + "prefix"
    })
    return result;
}

var target = transform({
    "key1": 1,
    "key2": 2,
})

// now target has a {"key1": "1prefix", "key2": "2prefix"}

var three = target.key3 // error here

@Strate
Copy link
Author

Strate commented Dec 15, 2015

This is not about string|number, this is more about keeping object shape replacing types of values. The real-world case is trannsforming object of plain strings to object of complex objects, i.e.:

let out = defineMessages("idPrefix.", {
    id1: "Some message"
})

out === {
    id1: {
        id: "idPrefix.id1",
        defaultMessage: "Some message"
    }
}

This function transforms {[index: string]: string} to {[index: string]: { id: string, defaultMessage: string }}. I want to keep keys in this case.

I don't know how this can be achieved :)

@myitcv
Copy link

myitcv commented Dec 15, 2015

@Strate see #4967 (comment) for a related question

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Jan 5, 2016
@mhegazy mhegazy closed this as completed Feb 20, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants