From 2e7e935b028784efdced34104964dad74b455fbf Mon Sep 17 00:00:00 2001 From: Martin Probst Date: Sat, 9 Dec 2017 16:47:14 +0100 Subject: [PATCH] fix(common): fix a Closure compilation issue. Closure Compiler cannot infer that the swtich statement is exhaustive, which causes it to complain that the method does not always return a value. Work around the problem by throwing an exception in the default case, and using the `: never` type to ensure the code is unreachable. --- packages/common/src/i18n/format_date.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/common/src/i18n/format_date.ts b/packages/common/src/i18n/format_date.ts index df2294a339..cfb952c82b 100644 --- a/packages/common/src/i18n/format_date.ts +++ b/packages/common/src/i18n/format_date.ts @@ -270,6 +270,13 @@ function getDateTranslation( return getLocaleDayPeriods(locale, form, width)[currentHours < 12 ? 0 : 1]; case TranslationType.Eras: return getLocaleEraNames(locale, width)[date.getFullYear() <= 0 ? 0 : 1]; + default: + // This default case is not needed by TypeScript compiler, as the switch is exhaustive. + // However Closure Compiler does not understand that and reports an error in typed mode. + // The `throw new Error` below works around the problem, and the unexpected: never variable + // makes sure tsc still checks this code is unreachable. + const unexpected: never = name; + throw new Error(`unexpected translation type ${unexpected}`); } }