-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Comments
There isn't a way to represent this in the type system today. |
Any plans to future? Maybe some existing issue? |
This may be close but the problem here is the value types of 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 |
This is not about
This function transforms I don't know how this can be achieved :) |
@Strate see #4967 (comment) for a related question |
I need to define a function, which accepts an object of this type:
and transforms that object, keeping the keys, but replaces a values:
and I also want to keep typechecking for that case. This is JS example:
Any way to do that?
The text was updated successfully, but these errors were encountered: