I recently had a few global variables that I was referencing from a TypeScript file that TypeScript marked with the following error:
Property 'msSpeechRecognition' does not exist on type 'Window & typeof globalThis'.
So I created added the following code into my env.d.ts (generated and referenced in tsconfig.json by create-vue):
declare const msSpeechRecognition: undefined | SpeechRecognitionStatic;
This didn't fix the problem. However, when I switched the code to the following:
declare var msSpeechRecognition: undefined | SpeechRecognitionStatic;
The error on the reference to msSpeechRecognition disappeared. I understand the differences between const and var in JavaScript, but what are the differences when using them in type declarations?
I couldn't reproduce this same issue when replacing SpeechRecognitionStatic with string, so I know it's something related to the SpeechRecognitionStatic type. Here is how it looked like (brought from @types/webspeechapi):
interface SpeechRecognitionStatic {
prototype: SpeechRecognition;
new (): SpeechRecognition;
}
The error indicates that
msSpeechRecognitionis being accessed from thewindowobject aswindow.msSpeechRecognition. You can only add something toglobalThisby declaring it withvar. If you want to useconstinstead, you will need to replace references towindow.msSpeechRecognitionwith justmsSpeechRecognition.