published under license CC4-BY
posted in category Software Development & Programming / JavaScript
posted at 24. Jul '23
Howto Fix TypeScript Error “Did you intend to use it as a type annotation?”
The most obvious way to try to assign a type to a TypeScript object is wrong and syntactically it renames the value of a key.
Wrong:
handleStuff: ({ file: File }) => void;
Error:
/app # npx tsc --noEmit && eslint ./**/*.ts{,x}
components/MyComponent.tsx:37:27 - error TS2842: 'File' is an unused renaming of 'file'. Did you intend to use it as a type annotation?
37 handleStuff: ({ file: File }) => void;
~~~~
components/MyComponent.tsx:37:33
37 handleStuff: ({ file: File }) => void;
~
We can only write a type for 'file' by adding a type for the entire parameter here.
Good:
handleStuff: ({ file }: { file: File }) => void;
Or you can define a separate type.
No error. That’s all.
Add Comment