tassign icon indicating copy to clipboard operation
tassign copied to clipboard

Interfaces with all optional fields mess things up

Open fljot opened this issue 7 years ago • 2 comments

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
});

fljot avatar May 29 '17 18:05 fljot