Open
Description
Hello.
Bad news: I found use case when your implementation fails to notify about incompatible types =(
Good news: there is solution =)
In our project we had some interface automatically generated (from protobuf files), so it happened that one interface had all fields optional.
Here's demo (reproducible on https://www.typescriptlang.org/play/index.html):
function tassign<T extends U, U>(target: T, ...source: U[]): T {
return Object.assign({}, target, ...source);
}
// via https://stackoverflow.com/a/41159188
function tassign2<T, K extends keyof T>(target: T, ...source: Pick<T, K>[]): T {
return Object.assign({}, target, ...source);
}
interface MyState {
field: string;
}
const state: MyState = {
field: "value"
};
interface ProtocolThing {
all?: string;
fields?: number;
optional?: boolean;
}
const valueFromProtocol: ProtocolThing = {
all: 'fields are optional'
};
// No error :( it assumes everything is fine
tassign(state, {
field: valueFromProtocol
});
// Argument of type '{ field: ProtocolThing; }' is not assignable to parameter of type 'Pick<MyState, "field">'.
// Types of property 'field' are incompatible.
// Type 'ProtocolThing' is not assignable to type 'string'.
tassign2(state, {
field: valueFromProtocol
});
Metadata
Metadata
Assignees
Labels
No labels