odata-query icon indicating copy to clipboard operation
odata-query copied to clipboard

Select within an expand on an optional property not compatible with keyof

Open wedgberto opened this issue 1 year ago • 0 comments

In this sample, Expand is not able to check that each entry in the select array is a key of the optional maybe property of the parent type. I'd love to be able to rely on this great library to check that I am only selecting properties that have been defined on my classes/interfaces and not be open to typos.

import { Expand } from 'odata-query';

interface child {
    id: string,
    name: string,
}
interface parent {
    definite: child,
    maybe?: child,
}

const compiles_with_a_definite_property: Expand<parent> = {
    definite: {
        select: ['id', 'name'],
    },
};

const does_not_compile_but_I_want_the_key_check: Expand<parent> = {
    maybe: {
        select: ['id', 'name'],
    },
};

const compiles_but_not_safe: Expand<parent> = {
    maybe: {
        select: 'id,naem,property_name_that_is_not_in_the_data_source,you_get_the_idea,api_wont_like_it',
    },
};

The majority of the sample has no errors but does_not_compile_but_I_want_the_key_check has the following:

Type '{ maybe: { select: string[]; }; }' is not assignable to type 'Expand'. The types of 'maybe.select' are incompatible between these types. Type 'string[]' is not assignable to type 'Select<child | undefined> | undefined'. Type 'string[]' is not assignable to type 'never[]'. Type 'string' is not assignable to type 'never'."

wedgberto avatar Apr 05 '23 19:04 wedgberto