Skip to content

chore: use TSTyche for type testing #2234

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/main-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
run: yarn test:ci:coverage

- name: Test Public Typings
run: yarn test:tsd
run: yarn test:types
Copy link
Author

@mrazauskas mrazauskas Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an example, you can state that TypeScript 5.4 and above is supported and run tests like this:

Suggested change
run: yarn test:types
run: yarn test:types --target '>=5.4'

The tests will run agains each minor release, currently: 5.4, 5.5, 5.6, 5.7, 5.8. New TypeScript versions are added automatically as soon as they are published to npm.


- name: E2E Testing
run: yarn test:e2e
Expand Down
9 changes: 0 additions & 9 deletions jest.config.types.js

This file was deleted.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"test:ci": "jest --ci --runInBand",
"test:ci:coverage": "yarn test:ci --coverage",
"test:e2e": "yarn workspaces foreach -p run test:e2e",
"test:tsd": "jest -c jest.config.types.js",
"test:all": "yarn test && yarn test:e2e && yarn test:tsd",
"test:types": "tstyche",
"test:all": "yarn test && yarn test:e2e && yarn test:types",
"lint:types": "tsc",
"lint:eslint": "eslint ./packages",
"lint:all": "yarn lint:eslint && yarn lint:types",
Expand Down Expand Up @@ -43,7 +43,6 @@
"@eslint/js": "^9.17.0",
"@size-limit/preset-small-lib": "^11.1.6",
"@swc/core": "^1.3.26",
"@tsd/typescript": "^4.9.5",
"@types/jest": "^29.4.0",
"@types/mock-fs": "^4.13.1",
"@types/node": "20.14.8",
Expand All @@ -57,7 +56,6 @@
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jest-environment-node-single-context": "^29.4.0",
"jest-runner-tsd": "^4.0.0",
"lerna": "^8.1.9",
"lint-staged": "^13.1.0",
"memory-fs": "^0.5.0",
Expand All @@ -72,6 +70,7 @@
"strip-ansi": "^6.0.1",
"swc-node": "^1.0.0",
"ts-jest": "^29.2.4",
"tstyche": "^4.0.0-beta.9",
"typescript": "^5.7.2",
"typescript-eslint": "^8.18.1"
},
Expand Down
34 changes: 17 additions & 17 deletions packages/conf/__typetests__/index.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import {
FallbackLocales,
LinguiConfig,
} from "@lingui/conf"
import { expectAssignable } from "tsd"
import { expect } from "tstyche"

// only required props
expectAssignable<LinguiConfig>({
expect({
locales: ["en", "pl"],
})
}).type.toBeAssignableTo<LinguiConfig>()

// all props
expectAssignable<LinguiConfig>({
expect({
catalogs: [
{
path: "<rootDir>/locale/{locale}/messages",
Expand All @@ -33,19 +33,19 @@ expectAssignable<LinguiConfig>({
tsExperimentalDecorators: false,
},
fallbackLocales: {} as FallbackLocales,
format: "po",
format: "po" as const,
Copy link
Author

@mrazauskas mrazauskas Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behaviour is the same as discussed here: https://github.com/lingui/js-lingui/pull/1622/files#r1178201302

More details in documentation: https://tstyche.org/reference/expect-api#type-widening

formatOptions: { origins: true, lineNumbers: true },
locales: [],
orderBy: "messageId",
orderBy: "messageId" as const,
pseudoLocale: "",
rootDir: ".",
runtimeConfigModule: ["@lingui/core", "i18n"],
runtimeConfigModule: ["@lingui/core", "i18n"] as [string, string],
sourceLocale: "",
service: { name: "", apiKey: "" },
})
}).type.toBeAssignableTo<LinguiConfig>()

// custom extractors
expectAssignable<LinguiConfig>({
expect({
locales: ["en", "pl"],
extractors: [
{
Expand All @@ -58,18 +58,18 @@ expectAssignable<LinguiConfig>({
) => {},
},
],
})
}).type.toBeAssignableTo<LinguiConfig>()

// runtimeConfigModule
expectAssignable<LinguiConfig>({
expect({
locales: ["en", "pl"],
runtimeConfigModule: ["./custom-i18n-config", "i18n"],
})
runtimeConfigModule: ["./custom-i18n-config", "i18n"] as [string, string],
}).type.toBeAssignableTo<LinguiConfig>()

expectAssignable<LinguiConfig>({
expect({
locales: ["en", "pl"],
runtimeConfigModule: {
i18n: ["./custom-config", "i18n"],
Trans: ["./custom-config", "Trans"],
i18n: ["./custom-config", "i18n"] as [string, string],
Trans: ["./custom-config", "Trans"] as [string, string],
},
})
}).type.toBeAssignableTo<LinguiConfig>()
1 change: 0 additions & 1 deletion packages/conf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
],
"devDependencies": {
"@lingui/jest-mocks": "*",
"tsd": "^0.26.1",
"unbuild": "^2.0.0"
}
}
65 changes: 30 additions & 35 deletions packages/core/__typetests__/index.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,56 @@
import { expectType } from "tsd"
import { i18n } from "@lingui/core"
import { expect } from "tstyche"

expectType<string>(i18n._("message.id"))
expectType<string>(
expect(i18n._("message.id")).type.toBe<string>()
expect(
i18n._({
id: "message.id",
message: "Message",
})
)
expectType<string>(
).type.toBe<string>()
expect(
i18n._(
"message.id",
{ name: "Tim" },
{ message: "Hello {name}", comment: "", formats: {} }
)
)
expectType<string>(
i18n._(
// @ts-expect-error you could not use message descriptor together with rest of params
{
id: "message.id",
message: "Message",
},
{ name: "Tim" },
{ message: "Hello {name}", comment: "", formats: {} }
)
).type.toBe<string>()
expect(i18n._).type.not.toBeCallableWith(
// cannot use message descriptor together with rest of params
{
id: "message.id",
message: "Message",
},
{ name: "Tim" },
{ message: "Hello {name}", comment: "", formats: {} }
)

expectType<string>(i18n.t("message.id"))
expectType<string>(
expect(i18n.t("message.id")).type.toBe<string>()
expect(
i18n.t({
id: "message.id",
message: "Message",
})
)
).type.toBe<string>()

expectType<string>(
expect(
i18n.t(
"message.id",
{ name: "Tim" },
{ message: "Hello {name}", comment: "", formats: {} }
)
)
).type.toBe<string>()

expectType<string>(
i18n.t(
// @ts-expect-error you could not use message descriptor together with rest of params
{
id: "message.id",
message: "Message",
},
{ name: "Tim" },
{ message: "Hello {name}", comment: "", formats: {} }
)
expect(i18n.t).type.not.toBeCallableWith(
// cannot use message descriptor together with rest of params
{
id: "message.id",
message: "Message",
},
{ name: "Tim" },
{ message: "Hello {name}", comment: "", formats: {} }
)

i18n.load("cs", {})
i18n.load({ cs: {} })
// @ts-expect-error this is an invalid call
i18n.load({ cs: {} }, {})
expect(i18n.load).type.toBeCallableWith("cs", {})
expect(i18n.load).type.toBeCallableWith({ cs: {} })
expect(i18n.load).type.not.toBeCallableWith({ cs: {} }, {})
Loading