You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 31, 2024. It is now read-only.
I'm not a maintainer but I've just looked at the index.d.ts file and it appears that the expected return value of the t/$t methods is VueI18n.TranslateResult which in turn is defined as type TranslateResult = string | LocaleMessages;
LocaleMessages is defined as interface LocaleMessages { [key: string]: LocaleMessageObject; } which is an indexable type, which strictly could be empty.
This means that your consuming code will yell at you if a string is expected - e.g. values in objects, calls to Error() etc as a call to t/$t may not be a string.
Digging a bit deeper into the t method call chain (in src/index.ts) it appears that it actually won't always return a string. This exception to a string type return occurs where this._translate is called inside _t into a prepared into the constant ret which can return null, so may that's why the return type is defined as any. Maybe this is an area for exploration and improvement to be stricter on the types defined so that there is always a string (or at least an empty string) to trickle back up the call chain to ensure the return value for t is always a string. In particular, there is a line in _translate which checks !isNull(res). I wonder if this can be refactored to a !== '' check instead.
The thing with typescript is that it enforces best practices with return values and it's best not to mix them wherever possible. For example if a function returns a string for its happy path then the null-ish value ought to be an empty string; not null or false or anything else.
Anyway, just my 2 cents. I don't really have a solution to offer but I hope this information is useful if one of the maintainers are able to investigate this.
LbISS, JuniorTour, kleinfreund, johanstroem, rostamiani and 11 moreSensanaty
Then changed type TranslateResult = string | LocaleMessages; to type TranslateResult = string;
Then npx patch-package vue-i18n
And to automatically patch typings for all team members added to postinstall in package.json:
"scripts": {
"postinstall": "patch-package"
}
It'll not work in ALL cases, but it's working for me.
Activity
Barry-Fisher commentedon Oct 17, 2018
I'm not a maintainer but I've just looked at the
index.d.ts
file and it appears that the expected return value of thet
/$t
methods isVueI18n.TranslateResult
which in turn is defined astype TranslateResult = string | LocaleMessages;
LocaleMessages
is defined asinterface LocaleMessages { [key: string]: LocaleMessageObject; }
which is an indexable type, which strictly could be empty.This means that your consuming code will yell at you if a string is expected - e.g. values in objects, calls to
Error()
etc as a call tot
/$t
may not be a string.Digging a bit deeper into the
t
method call chain (insrc/index.ts
) it appears that it actually won't always return a string. This exception to a string type return occurs wherethis._translate
is called inside_t
into a prepared into the constantret
which can return null, so may that's why the return type is defined as any. Maybe this is an area for exploration and improvement to be stricter on the types defined so that there is always a string (or at least an empty string) to trickle back up the call chain to ensure the return value fort
is always a string. In particular, there is a line in_translate
which checks!isNull(res)
. I wonder if this can be refactored to a!== ''
check instead.The thing with typescript is that it enforces best practices with return values and it's best not to mix them wherever possible. For example if a function returns a string for its happy path then the null-ish value ought to be an empty string; not null or false or anything else.
Anyway, just my 2 cents. I don't really have a solution to offer but I hope this information is useful if one of the maintainers are able to investigate this.
Update vue-i18n. Commernt: Cannot use vue-i18n types at the moment du…
darthf1 commentedon Aug 2, 2019
Is this still on the radar? :)
LbISS commentedon Sep 4, 2019
Still thousands ' as string'-s in thousands of projects... :)
ffxsam commentedon Sep 4, 2019
Quick note, using
.toString()
also works, and is probably more correct than type-casting.LbISS commentedon Sep 5, 2019
In the end i have used
patch-package
.Then changed
type TranslateResult = string | LocaleMessages;
totype TranslateResult = string;
Then
npx patch-package vue-i18n
And to automatically patch typings for all team members added to postinstall in package.json:
It'll not work in ALL cases, but it's working for me.
alexeigs commentedon Oct 4, 2019
You could also use
$tc
instead of$t
whenever you just need the translation without further ado as this function returns a string always.profispojka commentedon Dec 27, 2019
use this interfaces.d.ts filein root of your projecz
// Extend Vue with our custom types
import Vue from 'vue'
declare module 'vue/types/vue' {
interface Vue {
// == translations
$t: (name: string, attrs?: any) => string
$tc: (name: string, count: number, attrs?: any) => string
}
}
// Needs to be here
export {}
piktur commentedon Jan 22, 2020
Did the trick for me.
Add vue-i18n.d.ts to avoid toString() calls.
19 remaining items