In the code snippet below, there is a function named factory
that returns a Symbol of type Bob
. However, TypeScript appears to forget the type of the return value immediately, leading to an error when trying to assign the return value to variable test
one line later.
const bob: unique symbol = Symbol('bob')
type Bob = typeof bob
const factory = (): Bob => bob
const instance = factory()
const test: Bob = instance // Type 'symbol' is not assignable to type 'unique symbol'.
I understand that adding an explicit type signature can resolve this issue:
const instance: typeof bob = factory();
OR
const intance: Bob = factory();
Is there a workaround to address this problem without changing the variable instance
to have an explicit type signature?