-
-
Notifications
You must be signed in to change notification settings - Fork 859
Any way to use this with TypeScript and not have "as string" all over the codebase? #410
Comments
I'm not a maintainer but I've just looked at the
This means that your consuming code will yell at you if a string is expected - e.g. values in objects, calls to Digging a bit deeper into the 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. |
Is this still on the radar? :) |
Still thousands ' as string'-s in thousands of projects... :) |
Quick note, using |
In the end i have used
Then changed
It'll not work in ALL cases, but it's working for me. |
You could also use |
use this interfaces.d.ts filein root of your projecz // Extend Vue with our custom types // Needs to be here |
Did the trick for me. |
Add an alternate TypeScript definition for vue-i18n's t() function to avoid needing to call toString() all over the place to convert LocaleMessages objects to strings. The file came from this comment: kazupon/vue-i18n#410 (comment)
Just importing TranslateResult type from i18 library. |
@piktur Thank its working but now I had a problem with // vue-i18n.d.ts
import VueI18n, {
Path, Values, Locale,
} from 'vue-i18n/types'
/**
* Overloads VueI18n interface to avoid needing to cast return value to string.
* @see https://github.com/kazupon/vue-i18n/issues/410
*/
declare module 'vue-i18n/types' {
export default class VueI18n {
t(key: Path, locale: Locale, values?: Values): string;
t(key: Path, values?: Values): string;
}
}
declare module 'vue/types/vue' {
interface Vue {
$t: typeof VueI18n.prototype.t;
}
interface VueConstructor<V extends Vue = Vue> {
i18n: typeof VueI18n.prototype;
}
}
- export {}
+ export default VueI18n |
Thanks @Zummek |
we supported in Vue I18n v9. |
Except that, if I understand correctly, Vue i18n v9 is only compatible with Vue.js 3 and more. So this improvement will have to wait a loooong time before being mass tested. |
Any idea why this might be completely ignored by my IDE Webstorm? That is to say, the error is still shown. I have other types of my own that do work. |
Refer to this shim, create import VueI18n from 'vue-i18n/types'
/**
* Replace default export of vue-i18n
* @see https://github.com/Microsoft/TypeScript/issues/14080#issuecomment-1050833256
*/
export { VueI18n } then import it in import {
Path, Values, Locale
} from 'vue-i18n/types'
import { VueI18n } from './vue-i18n'
/**
* Overloads VueI18n interface to avoid needing to cast return value to string.
* @see https://github.com/kazupon/vue-i18n/issues/410
*/
declare module './vue-i18n' {
interface VueI18n {
t(key: Path, locale: Locale, values?: Values): string
t(key: Path, values?: Values): string
}
}
declare module 'vue/types/vue' {
interface Vue {
$t: typeof VueI18n.prototype.t
}
interface VueConstructor<V extends Vue = Vue> {
i18n: typeof VueI18n.prototype
}
}
export default VueI18n |
This is part question, part feature request.
In my code, I constantly have to typecast:
If I don't, I get this error: Type 'TranslateResult' is not assignable to type 'string'
Is there some workaround for this? Maybe something we can declare once, so it always returns string?
The text was updated successfully, but these errors were encountered: