What is “not assignable to parameter of type never” error in typescript?

前端 未结 10 2218
無奈伤痛
無奈伤痛 2020-12-04 15:08

Code is:

const foo = (foo: string) => {
  const result = []
  result.push(foo)
}

I get the following TS error:

[t

10条回答
  •  余生分开走
    2020-12-04 15:38

    This seems to be a recent regression or some strange behavior in typescript. If you have the code:

    const result = []
    

    Usually it would be treated as if you wrote:

    const result:any[] = []
    

    however, if you have both noImplicitAny FALSE, AND strictNullChecks TRUE in your tsconfig, it is treated as:

    const result:never[] = []
    

    This behavior defies all logic, IMHO. Turning on null checks changes the entry types of an array?? And then turning on noImplicitAny actually restores the use of any without any warnings??

    When you truly have an array of any, you shouldn't need to indicate it with extra code.

提交回复
热议问题