Closed eunicode closed 1 year ago
Okay, I got help from the Typescript Discord channel. When you declare a class in TypeScript, you also create a type of the instance of the class. I think the old Typescript docs make this clearer than the new Typescript docs. So this should work:
import Fuse from 'fuse.js'
let myFuse: Fuse | null = null
myFuse = new Fuse(data)
Is there a way to import the type of a Fuse instance? I don't seem to see it here: https://github.com/krisk/Fuse/blob/master/src/index.d.ts
A simplified example of the use case would be:
A real use case would be a React component that "caches" the Fuse instance. We do this by creating a variable will point to the cached Fuse instance reference after the component mounts. So we declare the
fuseRef
variable and initially set it to null. ThefuseRef
variable should have a type that says thefuseRef
variable value will either be null or a Fuse instance.