Skip to content
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

$t does not have TypeScript auto-completions #1215

Closed
5 tasks done
innocenzi opened this issue Nov 5, 2022 · 6 comments
Closed
5 tasks done

$t does not have TypeScript auto-completions #1215

innocenzi opened this issue Nov 5, 2022 · 6 comments
Labels
🍰 p2-nice-to-have Priority 2: nothing is broken but it's worth addressing Status: Proposal Request for comments typescript

Comments

@innocenzi
Copy link

Reporting a bug?

The $t global function in SFCs' templates don't seem to support global TypeScript definitions.

CleanShot 2022-11-05 at 23 02 35

The documentation regarding this topic doesn't mention $t but its definition look like it should support auto-completion.

Expected behavior

$t should provide auto-completion through the overriden DefineLocaleMessage interface, like t exported from useI18n does.

CleanShot 2022-11-05 at 23 03 33

Reproduction

Create a i18n.json file with the following content:

Details
{
	"en": {
		"login": {
			"welcome_text_title": "Welcome back!",
			"welcome_text_help_text": "Enter your credentials to access the app.",
			"email_field_placeholder": "Enter your email",
			"password_field_placeholder": "Enter your password",
			"sign_in_button_label": "Sign in",
			"quick_login_cta_text": "Wanna login quickly?",
			"quick_login_link_text": "Click here."
		}
	}
}

Create a i18n.ts file with the following content:

import { createI18n } from 'vue-i18n'
import messages from './i18n.json'

export type MessageSchema = typeof messages['en']

declare module 'vue-i18n' {
	export interface DefineLocaleMessage extends MessageSchema {}
}

const i18n = createI18n<[MessageSchema], 'en'>({
	locale: 'en',
	inheritLocale: true,
	messages,
})

export default i18n

Create a Vue 3 application, import ./i18n.ts and register the exported plugin.

System Info

System:
    OS: macOS 12.6
    CPU: (10) arm64 Apple M1 Pro
    Memory: 91.94 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.11.0 - /opt/homebrew/bin/node
    Yarn: 1.22.18 - /opt/homebrew/bin/yarn
    npm: 8.19.2 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 107.0.5304.87
    Firefox: 106.0.5
    Safari: 16.0
  npmPackages:
    @vitejs/plugin-vue: ^3.2.0 => 3.2.0 
    @vue/runtime-core: ^3.2.41 => 3.2.41 
    vite: ^3.2.2 => 3.2.2 
    vite-plugin-run: ^0.2.0 => 0.2.0 
    vue: ^3.2.41 => 3.2.41 
    vue-i18n: ^9.0.0 => 9.2.2

Screenshot

No response

Additional context

No response

Validations

@innocenzi innocenzi added the Status: Review Needed Request for review comments label Nov 5, 2022
@kazupon kazupon added 🍰 p2-nice-to-have Priority 2: nothing is broken but it's worth addressing typescript Type: Improvement Includes backwards-compatible fixes Status: Proposal Request for comments and removed Type: Improvement Includes backwards-compatible fixes Status: Review Needed Request for review comments labels Nov 15, 2022 — with Volta.net
@TheVaan
Copy link

TheVaan commented Mar 8, 2024

Any news about $t having TypeScript autocomplete?

@wJoenn
Copy link

wJoenn commented Apr 8, 2024

In the meantime you can use the non-global method and you'll have all the intellisense you need, just like in the script

<template>
  <div>{{ t("foo") }}</div>
</template>

<script setup lang="ts">
  const { t } = useI18n()
</script>

@beqacrc
Copy link

beqacrc commented Apr 25, 2024

Any updates?

@mitjans
Copy link
Contributor

mitjans commented Jun 28, 2024

I was looking for this feature, is there any update on this?

I am open to contributing!

@mitjans
Copy link
Contributor

mitjans commented Jun 28, 2024

When declaring types for $t in packages/vue-i18n/src/vue.d.ts, there's this parameter type Path added:

$t<...>(
  key: Key | ResourceKeys | Path
): TranslateResult

The definition of this type Path comes from packages/core-base/src/resolver.ts and is:

/** @VueI18nGeneral */
export type Path = string

This is breaking type inference for translations when using $t, $tc, and others. I've tried to remove | Path from all $t declarations and it works!

@kazupon
Copy link
Member

kazupon commented Jul 5, 2024

close via #1883 1883

@kazupon kazupon closed this as completed Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍰 p2-nice-to-have Priority 2: nothing is broken but it's worth addressing Status: Proposal Request for comments typescript
Projects
None yet
Development

No branches or pull requests

6 participants