简体   繁体   中英

ESLint error in VSCode - Parsing error: '?' expected

Here's the code in question:

type Params<T> = { 
  [K in keyof Nested<T>]: 
    (Parameters<
      Nested<T>[K] extends infer U extends (...args: any) => any ? U : never
    >) 
};

The error is on the U of infer U :

Parsing error: '?' expected.

The type seems to be inferred correctly, so I don't think it's a TS error. How to get VSCode to see it correctly?

You're attempting to add an extends constraint to an infer ed type variable:

type Params<T> = { 
  [K in keyof Nested<T>]: 
    (Parameters<
      Nested<T>[K] extends infer U extends (...args: any) => any ? U : never
//                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                    `extends` constraint on `infer`ed type variable
    >) 
};

The ability to do this was only introduced in TypeScript 4.7 ( reference ). You can verify it works in a TypeScript Playground configured to use TypeScript 4.7 here . And you can see a reproduction of your issue in a TypeScript Playground configured to use TypeScript 4.6 here .

If compilation works but VS Code is erroring, your project and VS Code may have been configured to use different versions of TypeScript. See VS Code's documentation on the compiler vs language service .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM