Skip to content

Commit 3ff8b1e

Browse files
authored
fix: support type inference of Translation, NumberFormat and DatetimeFormat components on SFC template and JSX/TSX (#1310)
* fix: support type inference of Translation, NumberFormat and DatetimeFormat components on SFC template and JSX/TSX * fix: timezone setting * fix: pinned node v18 minor version * fix * fix: disalbe node v18 related: nodejs/node#46123 related: nodejs/node#45171
1 parent 204bb2f commit 3ff8b1e

29 files changed

+208
-107
lines changed

.github/workflows/ci.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ jobs:
8080
strategy:
8181
matrix:
8282
os: [ubuntu-latest, windows-latest, macos-latest]
83-
node: [14, 16, 18]
83+
node: [14, 16]
84+
#node: [14, 16, 18.0]
8485

8586
runs-on: ${{ matrix.os }}
8687

@@ -142,7 +143,8 @@ jobs:
142143
strategy:
143144
matrix:
144145
os: [ubuntu-latest, windows-latest, macos-latest]
145-
node: [14, 16, 18]
146+
node: [14, 16]
147+
#node: [14, 16, 18.0]
146148

147149
runs-on: ${{ matrix.os }}
148150

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = {
5151

5252
// A path to a module which exports an async function that is triggered once before all test suites
5353
// globalSetup: null,
54-
globalSetup: require('set-tz')('UTC'),
54+
globalSetup: './jest.global.setup.js',
5555

5656
// A path to a module which exports an async function that is triggered once after all test suites
5757
// globalTeardown: null,

jest.global.setup.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = async () => {
2+
process.env.TZ = 'UTC'
3+
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"format:package": "node -r esbuild-register ./scripts/fixpack.ts",
4949
"format:prettier": "prettier --config .prettierrc --ignore-path .prettierignore --list-different '**/*.{js,json,html}'",
5050
"lint": "run-p lint:secret lint:codes lint:docs",
51-
"lint:codes": "eslint --cache ./packages ./test-d ./e2e ./benchmark --ext .js,.mjs,.ts,.vue",
51+
"lint:codes": "eslint --cache ./packages ./test-dts ./e2e ./benchmark --ext .js,.mjs,.ts,.vue",
5252
"lint:docs": "textlint --config .textlintrc.js docs/*.md docs/advanced/**/*.md docs/essentials/**/*.md docs/migration/**/*.md docs/api/injection.md packages/**/*.md",
5353
"lint:fix": "run-p \"lint:codes --fix\" \"lint:docs --fix\"",
5454
"lint:secret": "npx secretlint \"**/*\"",
@@ -59,7 +59,7 @@
5959
"test": "npm-run-all lint clean:cache:jest test:cover test:type test:e2e",
6060
"test:cover": "npm run test:unit -- --coverage",
6161
"test:e2e": "jest --runInBand --config ./jest.e2e.config.js",
62-
"test:type": "tsc -p ./test-d/tsconfig.json",
62+
"test:type": "tsc -p ./test-dts/tsconfig.json",
6363
"test:unit": "npm run clean:cache:jest && jest --env node"
6464
},
6565
"devDependencies": {

packages/vue-i18n-bridge/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,14 @@ export {
9898
} from '../../vue-i18n-core/src/i18n'
9999
export {
100100
Translation,
101+
I18nT,
101102
TranslationProps,
102103
BaseFormatProps,
103104
DatetimeFormat,
105+
I18nD,
104106
DatetimeFormatProps,
105107
NumberFormat,
108+
I18nN,
106109
NumberFormatProps
107110
} from '../../vue-i18n-core/src/components'
108111
export { I18nPluginOptions } from '../../vue-i18n-core/src/plugin'

packages/vue-i18n-bridge/src/runtime.ts

+3
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,14 @@ export {
9393
} from '../../vue-i18n-core/src/i18n'
9494
export {
9595
Translation,
96+
I18nT,
9697
TranslationProps,
9798
BaseFormatProps,
9899
DatetimeFormat,
100+
I18nD,
99101
DatetimeFormatProps,
100102
NumberFormat,
103+
I18nN,
101104
NumberFormatProps
102105
} from '../../vue-i18n-core/src/components'
103106
export { I18nPluginOptions } from '../../vue-i18n-core/src/plugin'

packages/vue-i18n-bridge/src/vue.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ import type {
2929
NumberFormatResult
3030
} from '../../vue-i18n-core/src/legacy'
3131
import type { ExportedGlobalComposer } from '../../vue-i18n-core/src/i18n'
32+
import type {
33+
Translation,
34+
DatetimeFormat,
35+
NumberFormat
36+
} from '../../vue-i18n-core/src/components'
3237

3338
declare module '@vue/runtime-core' {
3439
/**
@@ -1223,4 +1228,10 @@ declare module '@vue/runtime-core' {
12231228
key: Key | ResourceKeys
12241229
): LocaleMessageValue<VueMessageType> | {}
12251230
}
1231+
1232+
export interface GlobalComponents {
1233+
['i18n-t']: typeof Translation
1234+
['i18n-d']: typeof DatetimeFormat
1235+
['i18n-n']: typeof NumberFormat
1236+
}
12261237
}

packages/vue-i18n-core/src/components/DatetimeFormat.ts

+31-20
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
import { defineComponent } from 'vue'
2+
import { assign } from '@intlify/shared'
3+
import { DATETIME_FORMAT_OPTIONS_KEYS } from '@intlify/core-base'
14
import { useI18n } from '../i18n'
25
import { DatetimePartsSymbol } from '../symbols'
36
import { renderFormatter } from './formatRenderer'
47
import { baseFormatProps } from './base'
5-
import { assign } from '@intlify/shared'
6-
import { DATETIME_FORMAT_OPTIONS_KEYS } from '@intlify/core-base'
78

89
/* eslint-enable @typescript-eslint/no-unused-vars */
10+
import type { VNodeProps } from 'vue'
911
import type { DateTimeOptions } from '@intlify/core-base'
1012
import type { Composer, ComposerInternal } from '../composer'
1113
import type { FormattableProps } from './formatRenderer'
14+
import type { BaseFormatProps } from './base'
1215

1316
/**
1417
* DatetimeFormat Component Props
@@ -20,24 +23,7 @@ export type DatetimeFormatProps = FormattableProps<
2023
Intl.DateTimeFormatOptions
2124
>
2225

23-
/**
24-
* Datetime Format Component
25-
*
26-
* @remarks
27-
* See the following items for property about details
28-
*
29-
* @VueI18nSee [FormattableProps](component#formattableprops)
30-
* @VueI18nSee [BaseFormatProps](component#baseformatprops)
31-
* @VueI18nSee [Custom Formatting](../guide/essentials/datetime#custom-formatting)
32-
*
33-
* @VueI18nDanger
34-
* Not supported IE, due to no support `Intl.DateTimeFormat#formatToParts` in [IE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts)
35-
*
36-
* If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-datetimeformat)
37-
*
38-
* @VueI18nComponent
39-
*/
40-
export const DatetimeFormat = /* #__PURE__*/ /*defineComponent */ {
26+
export const DatetimeFormatImpl = /* #__PURE__*/ defineComponent({
4127
/* eslint-disable */
4228
name: 'i18n-d',
4329
props: assign(
@@ -77,4 +63,29 @@ export const DatetimeFormat = /* #__PURE__*/ /*defineComponent */ {
7763
(i18n as any)[DatetimePartsSymbol](...args)
7864
)
7965
}
66+
})
67+
68+
/**
69+
* Datetime Format Component
70+
*
71+
* @remarks
72+
* See the following items for property about details
73+
*
74+
* @VueI18nSee [FormattableProps](component#formattableprops)
75+
* @VueI18nSee [BaseFormatProps](component#baseformatprops)
76+
* @VueI18nSee [Custom Formatting](../guide/essentials/datetime#custom-formatting)
77+
*
78+
* @VueI18nDanger
79+
* Not supported IE, due to no support `Intl.DateTimeFormat#formatToParts` in [IE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts)
80+
*
81+
* If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-datetimeformat)
82+
*
83+
* @VueI18nComponent
84+
*/
85+
export const DatetimeFormat = DatetimeFormatImpl as unknown as {
86+
new (): {
87+
$props: VNodeProps & DatetimeFormatProps & BaseFormatProps
88+
}
8089
}
90+
91+
export const I18nD = DatetimeFormat

packages/vue-i18n-core/src/components/NumberFormat.ts

+36-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import { defineComponent } from 'vue'
2+
import { assign } from '@intlify/shared'
3+
import { NUMBER_FORMAT_OPTIONS_KEYS } from '@intlify/core-base'
14
import { useI18n } from '../i18n'
25
import { NumberPartsSymbol } from '../symbols'
36
import { renderFormatter } from './formatRenderer'
47
import { baseFormatProps } from './base'
5-
import { assign } from '@intlify/shared'
6-
import { NUMBER_FORMAT_OPTIONS_KEYS } from '@intlify/core-base'
78

9+
import type { VNodeProps } from 'vue'
810
import type { NumberOptions } from '@intlify/core-base'
911
import type { Composer, ComposerInternal } from '../composer'
1012
import type { FormattableProps } from './formatRenderer'
13+
import type { BaseFormatProps } from './base'
1114

1215
/**
1316
* NumberFormat Component Props
@@ -19,24 +22,7 @@ export type NumberFormatProps = FormattableProps<
1922
Intl.NumberFormatOptions
2023
>
2124

22-
/**
23-
* Number Format Component
24-
*
25-
* @remarks
26-
* See the following items for property about details
27-
*
28-
* @VueI18nSee [FormattableProps](component#formattableprops)
29-
* @VueI18nSee [BaseFormatProps](component#baseformatprops)
30-
* @VueI18nSee [Custom Formatting](../guide/essentials/number#custom-formatting)
31-
*
32-
* @VueI18nDanger
33-
* Not supported IE, due to no support `Intl.NumberFormat#formatToParts` in [IE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/formatToParts)
34-
*
35-
* If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-numberformat)
36-
*
37-
* @VueI18nComponent
38-
*/
39-
export const NumberFormat = /* #__PURE__*/ /* defineComponent */ {
25+
export const NumberFormatImpl = /*#__PURE__*/ defineComponent({
4026
/* eslint-disable */
4127
name: 'i18n-n',
4228
props: assign(
@@ -76,4 +62,34 @@ export const NumberFormat = /* #__PURE__*/ /* defineComponent */ {
7662
(i18n as any)[NumberPartsSymbol](...args)
7763
)
7864
}
65+
})
66+
67+
/**
68+
* export the public type for h/tsx inference
69+
* also to avoid inline import() in generated d.ts files
70+
*/
71+
72+
/**
73+
* Number Format Component
74+
*
75+
* @remarks
76+
* See the following items for property about details
77+
*
78+
* @VueI18nSee [FormattableProps](component#formattableprops)
79+
* @VueI18nSee [BaseFormatProps](component#baseformatprops)
80+
* @VueI18nSee [Custom Formatting](../guide/essentials/number#custom-formatting)
81+
*
82+
* @VueI18nDanger
83+
* Not supported IE, due to no support `Intl.NumberFormat#formatToParts` in [IE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/formatToParts)
84+
*
85+
* If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-numberformat)
86+
*
87+
* @VueI18nComponent
88+
*/
89+
export const NumberFormat = NumberFormatImpl as unknown as {
90+
new (): {
91+
$props: VNodeProps & NumberFormatProps & BaseFormatProps
92+
}
7993
}
94+
95+
export const I18nN = NumberFormat

0 commit comments

Comments
 (0)