Skip to content
46 changes: 46 additions & 0 deletions @commitlint/types/src/rules.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Tests for "as const" compatibility:
* https://github.com/conventional-changelog/commitlint/pull/4633
*
* These are compile-time type checks (no runtime behavior). If a line fails to compile, the
* associated type definition needs to be fixed.
*/

import { RuleConfigSeverity, type RulesConfig } from "./index.js";

const ERROR = RuleConfigSeverity.Error;

const _scopeEnumObject = [
ERROR,
"always",
{ scopes: ["foo", "bar"] as const },
] as const;
const _scopeEnumObjectCheck: Partial<RulesConfig> = {
"scope-enum": _scopeEnumObject,
};
void _scopeEnumObjectCheck;

// Simple array form: regression check that the array-form enum config
// remains assignable to RulesConfig when using `as const`.
const _scopeEnumSimple = [ERROR, "always", ["foo", "baz", "baz"]] as const;
const _scopeEnumSimpleCheck: Partial<RulesConfig> = {
"scope-enum": _scopeEnumSimple,
};
void _scopeEnumSimpleCheck;

const _scopeCaseObject = [
ERROR,
"always",
{ cases: ["camel-case"] as const, delimiters: ["-"] as const },
] as const;
const _scopeCaseObjectCheck: Partial<RulesConfig> = {
"scope-case": _scopeCaseObject,
};
void _scopeCaseObjectCheck;
Comment thread
Zamiell marked this conversation as resolved.

// Simple array form: ensure CaseRuleConfig accepts readonly arrays.
const _scopeCaseSimple = [ERROR, "always", ["camel-case"]] as const;
const _scopeCaseSimpleCheck: Partial<RulesConfig> = {
"scope-case": _scopeCaseSimple,
};
void _scopeCaseSimpleCheck;
14 changes: 10 additions & 4 deletions @commitlint/types/src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ export type RuleConfig<

export type CaseRuleConfig<V = RuleConfigQuality.User> = RuleConfig<
V,
TargetCaseType | TargetCaseType[]
TargetCaseType | readonly TargetCaseType[]
>;
export type LengthRuleConfig<V = RuleConfigQuality.User> = RuleConfig<
V,
number
>;
export type EnumRuleConfig<V = RuleConfigQuality.User> = RuleConfig<
V,
string[]
readonly string[]
>;
export type ObjectRuleConfig<
V = RuleConfigQuality.User,
Expand Down Expand Up @@ -115,12 +115,18 @@ export type RulesConfig<V = RuleConfigQuality.User> = {
"references-empty": RuleConfig<V>;
"scope-case":
| CaseRuleConfig<V>
| ObjectRuleConfig<V, { cases: TargetCaseType[]; delimiters?: string[] }>;
| ObjectRuleConfig<
V,
{ cases: readonly TargetCaseType[]; delimiters?: readonly string[] }
>;
"scope-delimiter-style": EnumRuleConfig<V>;
"scope-empty": RuleConfig<V>;
"scope-enum":
| EnumRuleConfig<V>
| ObjectRuleConfig<V, { scopes: string[]; delimiters?: string[] }>;
| ObjectRuleConfig<
V,
{ scopes: readonly string[]; delimiters?: readonly string[] }
>;
"scope-max-length": LengthRuleConfig<V>;
"scope-min-length": LengthRuleConfig<V>;
"signed-off-by": RuleConfig<V, string>;
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"outDir": "./lib"
},
"include": ["./src"],
"exclude": ["./lib/**/*"]
"exclude": ["./src/**/*.test-d.ts", "./lib/**/*"]
}
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./tsconfig.shared.json",
"files": [],
"include": ["./**/*.test.ts", "./**/*-test.ts"],
"include": ["./**/*.test.ts", "./**/*-test.ts", "./**/*.test-d.ts"],
"exclude": ["./**/lib/*.ts"],
"compilerOptions": {
"noEmit": true
Expand Down Expand Up @@ -29,6 +29,7 @@
{ "path": "@commitlint/prompt" },
{ "path": "@commitlint/cz-commitlint" },
{ "path": "@commitlint/config-conventional" },
{ "path": "@commitlint/config-pnpm-scopes" }
{ "path": "@commitlint/config-pnpm-scopes" },
{ "path": "@commitlint/types" }
]
}
3 changes: 3 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
typecheck: {
enabled: true,
},
exclude: ["**/node_modules/**", "**/lib/*.test.js"],
environment: "commitlint",
coverage: {
Expand Down
Loading