core: improve Translator type for strict TypeScript compatibility#2562
core: improve Translator type for strict TypeScript compatibility#2562lucas-koehler wants to merge 1 commit intomasterfrom
Conversation
Fix #2528 - Replace overloaded Translator type with a generic conditional type - Add createTranslator helper to allow implementation without type assertions under strictFunctionTypes and strictNullChecks.
✅ Deploy Preview for jsonforms-examples ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
I understand the intention of the change but personally I would like to do a different approach. The PR solves the typing issue by force-casting a simplified translator method to our own. However this means that a "wrongly" implemented translator which takes in a "string"-default parameter but still returns "undefined", is accepted by our official typing and used, without further checking, as if the requirement holds true. I see two different approaches:
What do you think? |
|
@sdirix Thanks for the feedback! While I generally like the narrowing effect of the generic type, I see your concern. I honestly did not think of the problem of implementation faults that "illegally" return
That might be a nice idea. We can just return the default value in any case if the called translator returns
I'm not sure what exactly you mean with that. Do you mean simplifying the export type Translator = {
(id: string, defaultMessage?: string, values?: any): string | undefined;
}; |
Fix #2528
undefineddefaultMessage results instring | undefinedwhile a string default message results instring.