English 中文(简体)
恢复“永远”功能,如果不是物体,则只缩小范围
原标题:Returning `never` for function that always throws only narrows when not an object property
  • 时间:2024-01-16 21:54:41
  •  标签:
  • typescript

让我说,你有某种功能,留下一个特别混淆的错误:

function throwMyError(): never {
    throw new Error("My Custom error")
}

你们有不可否认的价值:

declare const str: string | null

你们可以把这种价值缩小到像以下一点不可否认的东西:

if(!str) throwMyError()
str.toLocaleLowerCase() // fine

现在,这项职能在字面上说,它不能再作同样的缩小:

const foo = { throwMyError }
if (!str) foo.throwMyError()
str.toLocaleLowerCase() //  obj  is possibly  null .(18047)

或:

const bar = {
    throwMyError: (): never => {
        throw new Error()
    }
}
if (!str) bar.throwMyError()
str.toLocaleLowerCase() //  obj  is possibly  null .(18047)

为什么在从物体类型中要求投掷功能时,这种缩小并不奏效?

能否在不产生某种上游职能的情况下解决这一问题?

马达加斯加


相关问题: 类型缩小和取样;从来不履行的职能

但是,这个问题似乎还没有解决,因为在所有情况下,职能类别is都明确标明了返回的附加说明never

最佳回答

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. 与代码的游乐联系

问题回答

暂无回答




相关问题
store data in memory with nestjs

I am trying to persist some data in my nestjs server so I can then use that data in my client app through http requests. I have created a products.service.ts file with the function getAllData() that ...

React Hook Form Error on custom Input component

I am having a problem when I use react hook form + zod in my application, in short the inputs never change value and I get the following error in the console: Warning: Function components cannot be ...

Updatable promises using proxy in JavaScript

EDIT: I ve updated this question, and the old question is moved here. A POC can be found on this jsfiddle or the snippet below GOAL: The goal here is to make or rather simulate a promise that can be ...

热门标签