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.
This commit is contained in:
Martin Probst 2017-12-09 16:47:14 +01:00 committed by Alex Rickabaugh
parent b89e7c2cb7
commit 2e7e935b02
1 changed files with 7 additions and 0 deletions

View File

@ -270,6 +270,13 @@ function getDateTranslation(
return getLocaleDayPeriods(locale, form, <TranslationWidth>width)[currentHours < 12 ? 0 : 1];
case TranslationType.Eras:
return getLocaleEraNames(locale, <TranslationWidth>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}`);
}
}