contentful / field-editors

React components and extensions for building Contentful entry editor
https://contentful-field-editors.netlify.app/
MIT License
150 stars 118 forks source link

entityHelpers.getFieldValue returns wrong type #1140

Open BraunreutherA opened 2 years ago

BraunreutherA commented 2 years ago

Hi there,

it seems the https://github.com/contentful/field-editors/blob/master/packages/_shared/src/utils/entityHelpers.ts getFieldValue return the wrong type string | undefined. When the Field is not a string field it would return another type.

As we would like to use the method in our App, would it be possible to

  1. change the returned type to unknown | undefined

or

  1. use a generic type like

    export function getFieldValue<T = string>({
    /**
    * Expects an entity fetched with a flag Skip-Transformation: true
    */
    entity,
    fieldId,
    localeCode,
    defaultLocaleCode,
    }: {
    entity: {
    fields: { [key: string]: { [valueKey: string]: string | undefined } };
    };
    fieldId: string;
    localeCode: string;
    defaultLocaleCode: string;
    }): T | undefined {
    const values = get(entity, ['fields', fieldId]);
    if (!isObject(values)) {
    return;
    }
    
    const firstLocaleCode = Object.keys(values)[0];
    
    return values[localeCode] || values[defaultLocaleCode] || values[firstLocaleCode];
    }

Thanks

Alex

github-actions[bot] commented 2 years ago

Marking issue as stale since there was no acitivty for 30 days