I am having trouble using a partial on a generic type:
interface HasName {
name: string;
}
class Tools<T extends HasName> {
public create: (params: Partial<T>) => T;
createWithName = (nameArg: string) => {
this.create({ name: nameArg })
}
}
I expect this example to work because T extends HasName should ensure that T will have a name field, and (params: Partial<T>) should match any object with any subset of the keys of T.
However, there is a typescript compilation error on the line this.create({ name: nameArg }):
Argument of type '{ name: string; }' is not assignable to parameter of type 'Partial'.
Can someone help me understand why { name: string } is not assignable to Partial<T>, when it should be based on my logic above?
Thank you in advance.
public create: (params: Partial<HasName>) => T;create: <T extends HasName>(params: Partial<T>) => T;works though, strange issue.