Types in object destructuring

前端 未结 4 1276
你的背包
你的背包 2020-11-27 13:40

This

const { foo: IFoo[] } = bar;

and this

const { foo: Array } = bar;

will reasonably cause

4条回答
  •  爱一瞬间的悲伤
    2020-11-27 14:31

    NextJS Typescript example

    I had scenarios like so:

    const { _id } = req.query;
    if (_id.contains('whatever')) { // typescript complained
     ...
    }
    

    in which the "req.query" was typed like string | string[] from NextJS... so doing this worked:

    const { _id } = req.query as { _id: string };
    if (_id.contains('whatever')) { // typescript is fine
     ...
    }
    

    The irony of this is Typescript was correct but I don't want to do the actual programming work to handle strings and string arrays.

提交回复
热议问题