feat: support empty-array default in string syntax ( = [])#1636
Open
SynthLuvr wants to merge 3 commits into
Open
feat: support empty-array default in string syntax ( = [])#1636SynthLuvr wants to merge 3 commits into
= [])#1636SynthLuvr wants to merge 3 commits into
Conversation
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.
= [])
Contributor
There was a problem hiding this comment.
ℹ️ 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.tsshort-circuits[]to() => []so each traversal gets a fresh array. - Type-level parser in
parser/ast/default.tsaccepts[]as a default literal when the base type is array-like. - Type inference in
parser/ast/infer.tsmaps the[]literal tonever[]. - Tests in
__tests__/arrays/defaults.test.tsand__tests__/objects/defaults.test.tscover tuple literals, fresh references, equivalence to tuple thunks, trailing-junk rejection, and non-array base rejection.
Kimi K2 (free via Pullfrog for OSS) | 𝕏
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Resolves #1169
Summary
Allow defaulting a property to an empty array using the concise string
syntax, e.g.
type({ values: "string[] = []" }), instead of resorting tothe 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:
Implementation
parser/shift/operator/default.ts— runtimeparseDefaultshort-circuits an
[]token to a fresh-array thunk() => [], so eachtraversal that falls back to the default gets a brand-new array (matching
the existing tuple workaround). Type-level
parseDefaultaccepts[]and exports a new
inferDefaultLiteralalias.parser/ast/infer.ts—inferExpressionusesinferDefaultLiteralso an
[]default infers asnever[].parser/ast/default.ts—validateDefaultaccepts[], verifyingthe base type is array-like (
never[]assignable to the input type).The bundled
docs/components/dts/type.tspreview that was incidentallyregenerated during development has been reverted so it is not part of this
change.
Checklist
ark/type/__tests__/arrays/defaults.test.ts,ark/type/__tests__/objects/defaults.test.ts)type({ values: "string[] = []" })works