Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,22 @@ jobs:
- run: pnpm run build
- run: pnpm run test:coverage
- uses: codecov/codecov-action@v4.1.1

test-types:
runs-on: ubuntu-22.04
strategy:
matrix:
node: ['20']
steps:
- uses: actions/checkout@v4.1.1
- uses: pnpm/action-setup@v2.4.0
- name: Use Node.js
uses: actions/setup-node@v4.0.0
with:
node-version: ${{ matrix.node }}
check-latest: true
cache: 'pnpm'
- run: pnpm install
- run: pnpm run build
- run: pnpm run test:types

8 changes: 0 additions & 8 deletions jest.config.tsd.mjs

This file was deleted.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@scaleway/tsconfig": "workspace:*",
"@testing-library/jest-dom": "6.4.2",
"@testing-library/react": "14.2.2",
"@tsd/typescript": "5.3.3",
"@types/jest": "29.5.12",
"@types/node": "20.11.30",
"@types/react": "18.2.67",
Expand All @@ -41,7 +40,6 @@
"jest-environment-jsdom": "29.7.0",
"jest-junit": "16.0.0",
"jest-localstorage-mock": "2.4.26",
"jest-runner-tsd": "6.0.0",
"lint-staged": "15.2.2",
"mockdate": "3.0.5",
"prettier": "3.2.5",
Expand All @@ -52,7 +50,7 @@
"rollup-plugin-dts": "6.1.0",
"rollup-plugin-preserve-shebangs": "0.2.0",
"rollup-plugin-visualizer": "5.12.0",
"tsd-lite": "0.8.2",
"tstyche": "1.1.0 ",
"typescript": "5.4.3",
"wait-for-expect": "3.0.2"
},
Expand All @@ -66,7 +64,7 @@
"test": "TZ=UTC jest",
"test:watch": "pnpm run test --watch",
"test:coverage": "pnpm run test --coverage",
"test:types": "jest -c jest.config.tsd.mjs",
"test:types": "tstyche",
"release": "pnpm build && pnpm changeset publish",
"prepare": "husky install"
},
Expand Down
119 changes: 66 additions & 53 deletions packages/use-i18n/src/__typetests__/namespaceTranslation.test.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,69 @@
/* eslint-disable react-hooks/rules-of-hooks */
import { expectError, expectType } from 'tsd-lite'
import { expect, test } from 'tstyche'
import { useI18n } from '../usei18n'

const { namespaceTranslation } = useI18n<{
hello: 'world'
'doe.john': 'John Doe'
'doe.jane': 'Jane Doe'
'doe.child': 'Child is {name}'
'describe.john': '{name} is {age} years old'
}>()

// Single key
expectError(namespaceTranslation('hello'))

// Multiple keys
expectError(namespaceTranslation('doe.john'))
const scopedT1 = namespaceTranslation('doe')
expectType<string>(scopedT1('john'))
expectError(scopedT1('doesnotexists'))

// With a param
const scopedT2 = namespaceTranslation('doe')
expectError(scopedT2('child'))
expectType<string>(
scopedT2('child', {
name: 'Name',
}),
)
expectError(
scopedT2('doesnotexists', {
name: 'Name',
}),
)
expectError(
scopedT2('child', {
doesnotexists: 'Name',
}),
)
expectError(scopedT2('child', {}))
expectError(scopedT2('child'))

// With multiple params
const scopedT3 = namespaceTranslation('describe')
expectType<string>(
scopedT3('john', {
age: '30',
name: 'John',
}),
)
expectError(scopedT3('john', {}))
expectError(scopedT3('john'))

// Required generic
const { namespaceTranslation: namespaceTranslation2 } = useI18n()
expectError(namespaceTranslation2('test'))
test('i18n - namespaceTranslation', () => {
const { namespaceTranslation } = useI18n<{
hello: 'world'
'doe.john': 'John Doe'
'doe.jane': 'Jane Doe'
'doe.child': 'Child is {name}'
'describe.john': '{name} is {age} years old'
}>()

// Single key
expect(namespaceTranslation('hello')).type.toRaiseError(
`Argument of type '"hello"' is not assignable to parameter of type '"doe" | "describe"'`,
)

// Multiple keys
expect(namespaceTranslation('doe.john')).type.toRaiseError(
`Argument of type '"doe.john"' is not assignable to parameter of type '"doe" | "describe"'`,
)

expect(namespaceTranslation('doe')('john')).type.toEqual<string>()

expect(namespaceTranslation('doe')('doesnotexists')).type.toRaiseError(
`Expected 2 arguments, but got 1.`,
)

// With a param
expect(namespaceTranslation('doe')('child')).type.toRaiseError(
`Expected 2 arguments, but got 1.`,
)
expect(
namespaceTranslation('doe')('child', {
name: 'Name',
}),
).type.toEqual<string>()
expect(
namespaceTranslation('doe')('doesnotexists', {
name: 'Name',
}),
).type.toRaiseError()
expect(
namespaceTranslation('doe')('child', {
doesnotexists: 'Name',
}),
).type.toRaiseError()

expect(namespaceTranslation('doe')('child', {})).type.toRaiseError()
expect(namespaceTranslation('doe')('child')).type.toRaiseError()

// With multiple params
expect(
namespaceTranslation('describe')('john', {
age: '30',
name: 'John',
}),
).type.toEqual<string>()

expect(namespaceTranslation('describe')('john', {})).type.toRaiseError()
expect(namespaceTranslation('describe')('john')).type.toRaiseError()

// Required generic
const { namespaceTranslation: namespaceTranslation2 } = useI18n()
expect(namespaceTranslation2('test')).type.toRaiseError(
`Argument of type '"test"' is not assignable to parameter of type`,
)
})
100 changes: 50 additions & 50 deletions packages/use-i18n/src/__typetests__/t.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable react-hooks/rules-of-hooks */
import { expectError, expectType } from 'tsd-lite'
import { useI18n, useTranslation } from '../usei18n'
import { expect, test } from 'tstyche'
import { useI18n } from '../usei18n'

const { t } = useI18n<{
hello: 'world'
Expand All @@ -10,55 +9,56 @@ const { t } = useI18n<{
'describe.john': '{name} is {age} years old'
}>()

// Single key
expectType<string>(t('hello'))
expectError(t('keydoesnotexists'))
test('i18n - t', () => {
expect(t('hello')).type.toEqual<string>()
// Single key
expect(t('keydoesnotexists')).type.toRaiseError()

// Multiple keys
expectType<string>(t('doe.john'))
expectError(t('doe.doesnotexists'))
// Multiple keys
expect(t('doe.john')).type.toEqual<string>()
expect(t('doe.doesnotexists')).type.toRaiseError()

// With a param
expectError(t('doe.child'))
expectType<string>(
t('doe.child', {
name: 'Name',
}),
)
expectError(
t('doe.doesnotexists', {
name: 'Name',
}),
)
expectError(
t('doe.child', {
doesnotexists: 'Name',
}),
)
expectError(t('doe.child', {}))
expectError(t('doe.child'))
// With a param
expect(
t('doe.child', {
name: 'Name',
}),
).type.toEqual<string>()
expect(
t('doe.doesnotexists', {
name: 'Name',
}),
).type.toRaiseError()

// With multiple params
expectType<string>(
t('describe.john', {
age: '30',
name: 'John',
}),
)
expectError(t('describe.john', {}))
expectError(t('describe.john'))
expect(t('doe.child', {})).type.toRaiseError(
"Argument of type '{}' is not assignable to parameter of type",
)

// With react components as param value
expectType<string>(
t('doe.child', {
name: (
<p>
My name is<b>John</b>
</p>
),
}),
)
expect(t('doe.child')).type.toRaiseError('Expected 2 arguments, but got 1')

// Required generic
const { t: t2 } = useI18n()
expectError(t2('test'))
// With multiple params
expect(
t('describe.john', {
age: '30',
name: 'John',
}),
).type.toEqual<string>()

expect(t('describe.john', {})).type.toRaiseError()
expect(t('describe.john')).type.toRaiseError()

// With react components as param value
expect(
t('doe.child', {
name: (
<p>
My name is<b>John</b>
</p>
),
}),
).type.toEqual<string>()

// Required generic
const { t: t2 } = useI18n()
expect(t2('test')).type.toRaiseError()
})
10 changes: 10 additions & 0 deletions packages/use-i18n/src/__typetests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"strict": true,
"types": []
},
"include": ["./"],
"exclude": []
}
Loading