question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any way to use this with TypeScript and not have "as string" all over the codebase?

See original GitHub issue

This is part question, part feature request.

In my code, I constantly have to typecast:

this.$notify.error({
  title: this.$t('auth.invalidSignupTitle') as string,
  message: this.$t('general.error') as string,
});

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?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:51
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

66reactions
alexeigscommented, 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.

27reactions
pikturcommented, Jan 22, 2020
// 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 {}

Did the trick for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript: JavaScript With Syntax For Types.
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. Try TypeScript Now. Online or...
Read more >
Blocking usage of the any type in TypeScript codebases
Blocking usage of the any type in TypeScript codebases. The any type in TypeScript denotes that the value can be anything - string,...
Read more >
Why you should not use the type any in TypeScript
In JavaScript, you reassign any type to any variable, or even access attributes that do not exist. Not with TypeScript since TypeScript ......
Read more >
TypeScript: Stop Using 'any', There's a Type For That
Chances are you've worked with the `any` type in TypeScript. ... values from code that has been written without TypeScript or a 3rd...
Read more >
How To Use Basic Types in TypeScript - DigitalOcean
You can use the void type to define the variable in question as holding no type at all. If you assign the result...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found