A
A
Alex Khizhniy2017-06-13 11:25:34
JavaScript
Alex Khizhniy, 2017-06-13 11:25:34

Three dot typing in Typescript. How to resolve?

When using three dots to join objects typed by an interface, it does not throw an error when passing an invalid property that does not exist in this interface.
For example, is it possible to type this case?

interface IExampleInterface {
    property1: number;
    property2: string;
};

function test(props: IExampleInterface): IExampleInterface {
    return {
        ...props,
        property1: 123,
        property2: 'test',
        property3: 1 // Нет ошибки передачи
    };
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2017-06-13
@abberati

cast the returned object?

function test(props: IExampleInterface): IExampleInterface {
    return {
        ...props,
        property1: 123,
        property2: 'test',
        property3: 1 // Нет ошибки передачи
    } as IExampleInterface;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question