Skip to content

feat: support empty-array default in string syntax ( = [])#1636

Open
SynthLuvr wants to merge 3 commits into
arktypeio:mainfrom
SynthLuvr:feat/empty-array-default
Open

feat: support empty-array default in string syntax ( = [])#1636
SynthLuvr wants to merge 3 commits into
arktypeio:mainfrom
SynthLuvr:feat/empty-array-default

Conversation

@SynthLuvr

Copy link
Copy Markdown

Resolves #1169

Summary

Allow defaulting a property to an empty array using the concise string
syntax, e.g. type({ values: "string[] = []" }), instead of resorting to
the verbose tuple-with-initializer workaround.

Motivation

Quoting #1169 — in ~90% of cases where a property needs a default, the
default is an empty array, yet today you must write a lot of boilerplate:

scope({
  item: { foo: "string" },
  currently: ["item[]", "=", () => []],
  suggested: "item[] = []",
})

Implementation

  • parser/shift/operator/default.ts — runtime parseDefault
    short-circuits an [] token to a fresh-array thunk () => [], so each
    traversal that falls back to the default gets a brand-new array (matching
    the existing tuple workaround). Type-level parseDefault accepts []
    and exports a new inferDefaultLiteral alias.
  • parser/ast/infer.tsinferExpression uses inferDefaultLiteral
    so an [] default infers as never[].
  • parser/ast/default.tsvalidateDefault accepts [], verifying
    the base type is array-like (never[] assignable to the input type).

The bundled docs/components/dts/type.ts preview that was incidentally
regenerated during development has been reverted so it is not part of this
change.

Checklist

  • Tests added (ark/type/__tests__/arrays/defaults.test.ts,
    ark/type/__tests__/objects/defaults.test.ts)
  • type({ values: "string[] = []" }) works
  • Each traversal gets a fresh array

Enable defaulting a property to an empty array using concise string
syntax, e.g. `type({ values: "string[] = []" })`. Internally `[]` is
represented as a thunk `() => []` so each traversal that falls back to
the default gets a fresh array (matching the existing tuple workaround
`["string[]", "=", () => []]`).

Changes:
- parser/shift/operator/default.ts: runtime parseDefault short-circuits
  an `[]` token to a fresh-array thunk; type-level parseDefault accepts
  `[]` and exports a new `inferDefaultLiteral` alias.
- parser/ast/infer.ts: inferExpression uses `inferDefaultLiteral` so a
  `[]` default infers as `never[]`.
- parser/ast/default.ts: validateDefault accepts `[]`, verifying the
  base type is array-like (`never[]` assignable to the input type).
- docs/components/dts/type.ts: regenerate bundled type preview to
  reflect the new `inferDefaultLiteral` alias.
This bundled type preview was incidentally regenerated in 50947e6 and
should not be part of this change.
@github-project-automation github-project-automation Bot moved this to To do in arktypeio Jul 10, 2026
@SynthLuvr SynthLuvr changed the title feat: support empty-array default in string syntax () feat: support empty-array default in string syntax ( = []) Jul 10, 2026

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ℹ️ No critical issues — minor suggestions inline.

Reviewed changes — adds support for empty-array defaults in string syntax ("string[] = []") with runtime fresh-array thunks and type-level validation.

  • Runtime parser in parser/shift/operator/default.ts short-circuits [] to () => [] so each traversal gets a fresh array.
  • Type-level parser in parser/ast/default.ts accepts [] as a default literal when the base type is array-like.
  • Type inference in parser/ast/infer.ts maps the [] literal to never[].
  • Tests in __tests__/arrays/defaults.test.ts and __tests__/objects/defaults.test.ts cover tuple literals, fresh references, equivalence to tuple thunks, trailing-junk rejection, and non-array base rejection.

Pullfrog  | Fix all ➔Fix 👍s ➔View workflow run | Using Kimi K2 (free via Pullfrog for OSS) | 𝕏

Comment thread ark/type/__tests__/objects/defaults.test.ts Outdated
Add a matching .throws.snap(...) to the 'empty array default rejects
non-array base' test so it verifies both the runtime parse error and the
type-level error, consistent with the surrounding invalid-default tests
(incorrect default type, non-literal, fails on expression value).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To do

Development

Successfully merging this pull request may close these issues.

String definitions for empty tuple [] and for default empty array = []

1 participant