Open yannbriancon opened 2 years ago
related #1113 #1025
The essential improvement is to use conditional exports or the volar plugin.
Any updates on this @kazupon? Would really help the DX in nuxt-i18n :)
Here is workaround to fix type autocomplete locally
// typing.d.ts
interface ITranslations {
form: {
labels: {
email: string;
password: string;
};
errors: {
email: string;
minLength: string;
required: string;
};
};
signIn: {
heading: string;
submit: string;
};
}
type Primitive = string | number | bigint | boolean | undefined | symbol;
export type PropertyStringPath<T, Prefix = ''> = {
[K in keyof T]: T[K] extends Primitive | Array<unknown>
? `${string & Prefix}${string & K}`
: `${string & Prefix}${string & K}` | PropertyStringPath<T[K], `${string & Prefix}${string & K}.`>;
}[keyof T];
declare module 'vue-i18n' {
export interface DefineLocaleMessage extends ITranslations {}
}
declare module '@vue/runtime-core' {
import type { TranslateResult } from 'vue-i18n';
export interface ComponentCustomProperties {
$t<Key extends PropertyStringPath<ITranslations>>(key: Key): TranslateResult;
}
}
Here is workaround to fix type autocomplete locally
// typing.d.ts interface ITranslations { form: { labels: { email: string; password: string; }; errors: { email: string; minLength: string; required: string; }; }; signIn: { heading: string; submit: string; }; } type Primitive = string | number | bigint | boolean | undefined | symbol; export type PropertyStringPath<T, Prefix = ''> = { [K in keyof T]: T[K] extends Primitive | Array<unknown> ? `${string & Prefix}${string & K}` : `${string & Prefix}${string & K}` | PropertyStringPath<T[K], `${string & Prefix}${string & K}.`>; }[keyof T]; declare module 'vue-i18n' { export interface DefineLocaleMessage extends ITranslations {} } declare module '@vue/runtime-core' { import type { TranslateResult } from 'vue-i18n'; export interface ComponentCustomProperties { $t<Key extends PropertyStringPath<ITranslations>>(key: Key): TranslateResult; } }
Thanks for that, I ended up doing exactly that just with a little bit less TS magic:
// types.d.ts
import {
DefineLocaleMessage,
} from 'vue-i18n'
import type enUS from './locales/en-US.json'
type MessageSchema = typeof enUS
declare module 'vue-i18n' {
export interface DefineLocaleMessage extends MessageSchema {}
}
Reporting a bug?
When using global definitions and $t, key completion (and type checking) does not work.
Here is the declaration file I use:
I set it up like this:
Then in a component I try to type in but nothing appears:
Expected behavior
Resource completion should work if I understood the doc correctly. Should type checking work too?
Reproduction
Enough information.
System Info
Screenshot
No response
Additional context
No response
Validations