You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constlanguages=['de','en','fr']asconst;typeLanguage=(typeoflanguages)[number];constfallback='lorem';constcustomByLanguage={de: 'ipsum',}satisfiesPartial<Record<Language,string>>functiongetValue(language: Language): string{returncustomByLanguage[language]??fallback;// ts error: Element implicitly has an 'any' type because expression of type '"de" | "en" | "fr"' can't be used to index type '{ de: string; }'.// Property 'en' does not exist on type '{ de: string; }'.(7053)}
This error seems counter intuitive as a fallback if the property doesn't match was given via ?? fallback. Right now I need to add either typescript or javascript boilerplate to get rid of this error.
satisfies is not the same as an inferred or explicitly annotated type. customByLanguage has the more specific type of { de: string } and no index or record signature. If you want the wider type, just use that:
🔍 Search Terms
Example:
This error seems counter intuitive as a fallback if the property doesn't match was given via
?? fallback
. Right now I need to add either typescript or javascript boilerplate to get rid of this error.playground
✅ Viability Checklist
⭐ Suggestion
I'd expect typescript to not complain about lack of perfect key overlap if the nullish operator is being used to fallback if they key does not exist.
A bonus would be, if typescript still complained if key and object had no overlap at all, as that seems to be most likely an error.
💻 Use Cases
Now having to work with strict property access due to removal of
suppressImplicitAnyIndexErrors: false
in typescript 5.6The text was updated successfully, but these errors were encountered: