-
Notifications
You must be signed in to change notification settings - Fork 118
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
Type that adds a property #177
Comments
@mightyiam is there a difference why you can't use // also, you say "for a single property" but your example can add multiple properties, was this intended? For example |
It's for a usecase where N is inferred from a value const AddProperty = <T, V, N extends string>(object: T, value: V, name: N): Add<T,V,N> = {
return {
...object,
[name]: value
}
} |
You could still write the above example's return type as @krzkaczor what do you think, is it worth sucha a "convenience alias"? I'm not 100% sure, because this really is just a different way to call |
It's meant for a single property. Does anyone happen to know how that could be implemented? |
Exactly the same, only with some more complex requirements on what |
Hello, I think the following would give a better intellisense when hovering on VSCode: export type Add<T, V, N extends string | symbol> = { [P in keyof T | N]: P extends N ? T[P] & V : T[P] } |
Shouldn't it be type Add<T, V, N extends string | symbol> = { [P in keyof T | N]: P extends N ? V : T[P] } |
@gomain The type proposed by OP was type Add<T, V, N extends string | symbol> = T & { [P in N]: V } Therefore to be equivalent, it should be the way I proposed it. |
Thanks for the proposal but I think it's better to keep API smaller with only |
Would this type be welcome?
Similar to
Merge
, but for a single property.Not certain regarding the name.
@gomain
The text was updated successfully, but these errors were encountered: