Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript Types #274

Closed
NickBolles opened this issue Nov 27, 2018 · 27 comments
Closed

Typescript Types #274

NickBolles opened this issue Nov 27, 2018 · 27 comments

Comments

@NickBolles
Copy link
Contributor

What problem does this feature solve?

Typescript types for $auth on the vue instance

This feature request is available on Nuxt community (#c219)
@ghost ghost added the cmty:feature-request label Nov 27, 2018
@warheartvik
Copy link

I run into the same issue. For testing sake, I use some like this.

    //@ts-ignore
    if (this.$auth.loggedIn) {
        this.$router.push('/');
    }

but I don't recomend to use it all over the app, the code will look like crap.

@NickBolles
Copy link
Contributor Author

NickBolles commented Dec 1, 2018

WIP Types

Make sure you Replace User with your user object. I'm working on making this generic for the types to be included with the auth-module npm package.

/**
 * Augment the typings of Vue.js
 */

import Vue, { ComponentOptions } from "vue";
import { CookieAttributes } from "js-cookie";
import { User } from "../../server/services/v1/users/users.interface";

// todo: context?
interface StorageCookieOptions extends CookieAttributes {
  cookie: {
    prefix: string;
    options?: StorageCookieOptions;
  };
}
interface Storage {
  setUniversal(key: string, value: any, isJson?: boolean): string;
  getUniversal(key: string, isJson?: boolean): any;
  syncUniversal(key: string, defaultValue: any, isJson?: boolean): any;
  // Local State
  setState(key: string, val: any): string;
  getState(key: string): string;
  watchState(key: string, handler: (newValue: any) => void);
  // Cookies
  setCookie(key: string, val: any, options?: StorageCookieOptions);
  getCookie(key: string, isJson?: boolean): any;
  // Local Storage
  setLocalStorage(key: string, val: any, isJson?: boolean);
  getLocalStorage(key: string, isJson?: boolean): any;
}

interface Auth<T = any> {
  ctx: any;
  $state: any; // todo: type this
  $storage: Storage;
  user: Partial<T>;
  loggedIn: boolean;
  loginWith(strategyName: string, ...args): Promise<never>;
  login(...args): Promise<never>;
  logout(): Promise<never>;
  fetchUser(): Promise<never>;
  fetchUserOnce(): Promise<never>;
  hasScope(scopeName: string): boolean;
  setToken(strategyName: string, token?: string): string;
  syncToken(strategyName: string): string;
  onError(handler: (error: Error, name: string, endpoint: any) => void);
  setUser(user?: Partial<T>);
  reset(): Promise<never>;
  redirect(name: string);
}

declare module "vue/types/options" {
  interface ComponentOptions<V extends Vue> {
    auth?: boolean;
  }
}

declare module "vue/types/vue" {
  interface Vue {
    $auth: Auth<User>;
  }
}

updated 12/18

@NickBolles
Copy link
Contributor Author

Please post suggestions for improvements if you have any, I'll put a PR together at some point, I think there are a few issues with these typings that need to be fixed before that though.

@SolarLiner
Copy link

Using Promise<never> implies that the Promise is never resolved. I think you wanted to write Promise<void>, which translates to a promises which resolves without a value.

@ryanwinchester
Copy link

@NickBolles I'm a typescript noob. Where do I put that code?

@danielroe
Copy link
Member

@ryanwinchester Add it to a file called index.d.ts in your root directory.

Alternatively, you could also set up a types directory with a number of type declaration files, in which case I would add this content as another file in that directory, making sure it was listed in tsconfig.json under compilerOptions.typeRoots.

@clementmas
Copy link

What's the status on this?

Can it be supported by default?
Ideally we just have to add @nuxtjs/auth to the types array of tsconfig.json like most other modules.

@jonnyparris
Copy link

jonnyparris commented Sep 11, 2019

FYI - made a PR to definitely typed using @NickBolles's approach: DefinitelyTyped/DefinitelyTyped#38132

@laflo
Copy link

laflo commented Sep 11, 2019

FYI - made a PR to definitely typed using @NickBolles's approach: DefinitelyTyped/DefinitelyTyped#38132

I think the Promises should be of type Promise<void> instead of type Promise<never> as mentioned above. Correct me if I'm wrong.

@NickBolles
Copy link
Contributor Author

@jonnyparris Looks like that PR is merged now. Should we close this?

@jonnyparris
Copy link

@NickBolles sure
@laflo feel free to make a PR there too, sorry I missed it first time around

@steklopod
Copy link

Guys, please explain how to include this type.
Can we now add @nuxtjs/auth to the types array of tsconfig.json like most other modules.?

@jonnyparris
Copy link

Almost; you need to add @types/nuxtjs__auth to the tsconfig.json > compilerOptions > types array.

Make sure you've also added @types/nuxtjs__auth as a devDependency in your project.

@NickBolles
Copy link
Contributor Author

I submitted a PR to make this more automatic. See #486

@Greenfig
Copy link

Greenfig commented Jun 8, 2020

To resolve this.$auth in vuex typescript modules, I had to add the following

types/vue-shim.d.ts better to add to new file types/vue-nuxtauth.d.ts

import { Auth } from '@nuxtjs/auth'

declare module 'vuex/types/index' {
    interface Store<S> {
        $auth: Auth,
    }
}

@Micka33
Copy link

Micka33 commented Jul 5, 2020

I still have this issue in my NuxtJS project...

 ERROR  ERROR in @/layouts/app.vue(109,12):
109:12 Property '$auth' does not exist on type 'CombinedVueInstance<Vue, AppData, {....}, {....}, Readonly<unknown>>'.
    107 |     },
    108 |     logout() {
  > 109 |       this.$auth.logout()
        |            ^
    110 |         .then(() => {
    111 |           let logoutUrl = new URL(`https://${this.$auth.strategies.auth0.options.domain}/v2/logout`);
    112 |           let { origin } = new URL(document.URL);

Not sure what to do to fix it...

@Greenfig
Copy link

Greenfig commented Jul 5, 2020

@Micka33 did you add "@types/nuxtjs__auth" to your types array in the tsconfig.json?

@Micka33
Copy link

Micka33 commented Jul 6, 2020

Just did it, and my editor displays the following:
Screenshot 2020-07-06 at 11 57 41

Should install something more than @nuxtjs/auth ?
EDIT:
I ran npm install @types/nuxtjs__auth and my editor is not showing any error in tsconfig.json anymore.

@Onefivefournine
Copy link

If you need just to add typings for this.$auth (and context.$auth), add this in your type declaration file

import Auth from '@nuxtjs/auth/lib/core/auth'

declare module 'vue/types/vue' {
  interface Vue {
    $auth: Auth
  }
}

@SvenC56
Copy link

SvenC56 commented Nov 13, 2020

For @nuxtjs/auth-next my vue-nuxtauth.d.ts looks the following:

import Auth from '@nuxtjs/auth-next/dist/core/auth'

declare module 'vue/types/vue' {
  interface Vue {
    $auth: Auth
  }
}

@fahmiegerton
Copy link

import Auth from '@nuxtjs/auth-next/dist/core/auth'

declare module 'vue/types/vue' {
  interface Vue {
    $auth: Auth
  }
}

Hi, mind if i ask where to put this? should i put this in store folder? thanks

@danielroe
Copy link
Member

@fahmiegerton You could put that in an index.d.ts file in your source directory.

@fahmiegerton
Copy link

@fahmiegerton You could put that in an index.d.ts file in your source directory.

@danielroe I couldn't find any file named index.d.ts in my root project. Should I create this file first? Thanks

@danielroe
Copy link
Member

@fahmiegerton Yes 👍

@fahmiegerton
Copy link

@fahmiegerton Yes 👍

Oh okay, thank you very much!

@martinsagat
Copy link

This works for me:
vue-nuxtauth.d.ts :

import { Auth } from '@nuxtjs/auth-next'

declare module 'vue/types/vue' {
interface Vue {
$auth: Auth
}
}

then don't forget to include the file in the tsconfig.json

{
"include": [
"vue-nuxtauth.d.ts"
]
}

@udyux
Copy link

udyux commented Aug 11, 2023

I'm running into an issue with the generic argument for the Auth type on components in our Nuxt app. We have this in our Vue augmentation:

declare module 'vue/types/vue' {
  interface Vue {
    // ...
    $auth: Auth<SettingsDto>;
  }
}

In components however, typescript is picking up our augmentation as well as the types module augmentation which doesn't provide a generic type for Auth causing it to fallback to any. In VSCode I can see both being picked up but it seems to prioritize the module's definition. The result is the $auth.user property is always Partial<any>.

Any ideas on how I could fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests