Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript reduce return wrong Type and also forbid to assert it

Faced very strange ts behaviour.

type Labels = Record<string, boolean | undefined>

type LabelGroups = Record<string, Labels>

export function reduceLabelGroups(labelGroups: LabelGroups) {
    const labels = Object.entries(labelGroups).reduce((valueMap, [, value]) => ({...valueMap, ...value}), {});

    return labels;
}

It should return Labels Type after reduce – but it returns just {}
If you try to assert Labels like a

(…{} as Labels);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

it throws error "Unnecessary cast: Array#reduce accepts a type parameter for the default value."
How can I return type without any "as unknown as Labels" after return?

>Solution :

It’s just as the error says – .reduce accepts a type parameter, so use it.

export function reduceLabelGroups(labelGroups: LabelGroups) {
    return Object.entries(labelGroups).reduce<Labels>((valueMap, [, value]) => ({...valueMap, ...value}), {});
}

Might be more appropriate to use Object.values, since you’re not using the keys.

export function reduceLabelGroups(labelGroups: LabelGroups) {
    return Object.values(labelGroups).reduce<Labels>((valueMap, value) => ({...valueMap, ...value}), {});
}

Or perhaps avoid .reduce entirely, it’s not so appropriate here

export function reduceLabelGroups(labelGroups: LabelGroups) {
    const valueMap: Labels = {};
    for (const value of Object.values(labelGroups)) {
        Object.assign(valueMap, value);
    }
    return valueMap;
};

You can do shorter, but it requires a type assertion.

export function reduceLabelGroups(labelGroups: LabelGroups) {
    return Object.assign({}, ...Object.values(labelGroups)) as Labels;
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading