The conditions in which a never
-returning function is treated as an assertion that affects control flow analysis are spelled out in the implementing PR, microsoft/TypeScript#32695. It looks like this:
A function call is analyzed as an assertion call or never-returning call when
该呼吁作为最高级别的表述,
该呼吁具体规定了功能名称的单一识别标志或标明的识别序列,以及
each identifier in the function name references an entity with an explicit type, and
功能名称是指带有<代码> > 回归类型或明确<代码>的返回类型。
An entity is considered to have an explicit type when it is declared as a function, method, class or namespace, or as a variable, parameter or property with an explicit type annotation. (This particular rule exists so that control flow analysis of potential assertion calls doesn t circularly trigger further analysis.)
您对<代码>foo和bar
的回答是,它们是没有明确的never-returning功能;据说这会导致检查人员减速和循环警告的类型。
如果您希望<条码>foo和bar
工作,那么你就象这样需要明确通知他们:
declare const str: string | null;
const foo: { throwMyError(): never } = { throwMyError };
if (!str) foo.throwMyError();
str.toLocaleLowerCase(); // okay
or
declare const str: string | null;
const bar: { throwMyError(): never } = {
throwMyError: (): never => { throw new Error() }
};
if (!str) bar.throwMyError();
str.toLocaleLowerCase(); // okay
这是你想要的行为,例如,这里的法典并没有太多的额外工作。 但在实践中,这种关于类型说明的限制使用<代码>never-returning and 。 方法是一个巨大的负担,因为你发现,迫切需要提出明确姓名,以说明本来可以轻易匿名和推定的类型。 视使用情况而定,这从小的异常(const foo: Foo = ⋯
到完全的突破体(const baz: Baz<Map<string, boolean>“qux”、“[编号、“hello”]、Baz<Map<string, Annual>“quux”, [boolean, “goodbye”], ⋯>> =⋯
。 因此谨慎行事。
马达加斯加 4. 与代码的游乐联系