-
Notifications
You must be signed in to change notification settings - Fork 927
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
Comments
I run into the same issue. For testing sake, I use some like this.
but I don't recomend to use it all over the app, the code will look like crap. |
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 |
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. |
Using |
@NickBolles I'm a typescript noob. Where do I put that code? |
@ryanwinchester Add it to a file called 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 |
What's the status on this? Can it be supported by default? |
FYI - made a PR to definitely typed using @NickBolles's approach: DefinitelyTyped/DefinitelyTyped#38132 |
I think the Promises should be of type |
@jonnyparris Looks like that PR is merged now. Should we close this? |
@NickBolles sure |
Guys, please explain how to include this type. |
Almost; you need to add Make sure you've also added |
I submitted a PR to make this more automatic. See #486 |
To resolve
|
I still have this issue in my NuxtJS project...
Not sure what to do to fix it... |
@Micka33 did you add "@types/nuxtjs__auth" to your types array in the tsconfig.json? |
If you need just to add typings for import Auth from '@nuxtjs/auth/lib/core/auth'
declare module 'vue/types/vue' {
interface Vue {
$auth: Auth
}
} |
For 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 |
@fahmiegerton You could put that in an |
@danielroe I couldn't find any file named |
@fahmiegerton Yes 👍 |
Oh okay, thank you very much! |
This works for me: import { Auth } from '@nuxtjs/auth-next' declare module 'vue/types/vue' { then don't forget to include the file in the tsconfig.json { |
I'm running into an issue with the generic argument for the 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 Any ideas on how I could fix this? |
What problem does this feature solve?
Typescript types for $auth on the vue instance
The text was updated successfully, but these errors were encountered: