I'm using a library that exposes an SDK, but not the type (UserModule
) of one of the objects (user
) you can create with it.
import { Magic } from 'magic-sdk';
// ...
function useMagicLinkAuthentication() {
const [magicReady, setMagicReady] = usePromise<Magic>();
const [isMagicInitialized, setMagicInitialized] = useState(false);
const [user, setUser] = useState<UserModule | null>(null);
// ...
const signIn = async (email: string) => {
const magic = await magicReady;
if (await magic.user.isLoggedIn()) {
return setUser(magic.user);
}
await magic.auth.loginWithMagicLink({ email });
return setUser(magic.user);
};
// ...
}
As you can see, I need access to the UserModule
in useState
which I have no access to (because I can't do import { UserModule } from 'magic-sdk';
.
For functions there is ReturnType
. So if I have a regular function, I can do:
export const createFoo = () => { /* ... returns a Foo ... */ }
export type Foo = ReturnType<typeof createFoo>;
Is there an equivalent for classes in TypeScript so that I can access the instance of new MyClass
and get
// broken pseudo-code
export type Foo = Something<typeof MyClass.foo>
?
I need to somehow get typeof magic.user
aka the UserModule
type.
You can use a type
alias from the constructor:
type UserModule = Magic['user'];