0

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.

1

1 Answer 1

1

You can use a type alias from the constructor:

type UserModule = Magic['user'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.