Repository navigation
feat(cli): add --enum-style const to types and generate - #2070
Merged
Merged
Conversation
TypeScript enums are not erasable syntax, so the .ts files that types and generate write are rejected by erasableSyntaxOnly and by Node's type stripping. The const style emits an as-const object plus a same-named union type, which keeps X.MEMBER and X-as-a-type working for consumers. A .d.ts destination gets a declared const instead, since an ambient const cannot carry an object initializer. The default stays enum so existing output does not change. Fixes appwrite/sdk-for-cli#277
🟢 Tier S · Ready to merge
Adds an opt-in
📂 Walkthrough · 12
Merged |
4 tasks
2 tasks done
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.
What
Adds an opt-in
--enum-style enum|constflag toappwrite types(TypeScript only) andappwrite generate. The default staysenum, so existing output is byte-for-byte unchanged.With
--enum-style const, an enum column is emitted as anas constobject plus a union type of the same name:Consumers keep both usages they had with
enum:ItemsRunItemOrigin.CARRYOVERas a value andItemsRunItemOriginas a type. Plain string literals such as'adhoc'are now assignable too.Why
enumis not erasable syntax. A.tsfile containing one failstsc --erasableSyntaxOnly(TS1294) and Node's built-in type stripping refuses to load it (ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX). That hitsappwrite types ./src/appwrite.tsand everyappwrite generateoutput, sincegeneratewritestypes.ts.Design notes
.d.tsdestinations. The issue's form cannot go into a declaration file: an ambientconstmay not carry an object initializer (TS1254). When thetypesdestination ends in.d.ts,.d.mtsor.d.cts(including the defaultappwrite.d.ts), the const style emitsexport declare const X: { readonly A: "a"; ... };with the same union type. The defaultenumoutput in a.d.tsis left alone; tsc does not flag ambient enums undererasableSyntaxOnly.generateis included. The generateddatabases.tsonly uses the enum names as types, never their members, so swapping the declaration form changes nothing else. Both commands now render through the sametypegen.TypeScript.DeclareEnum, which replaces the duplicated enum printer ingenerator/typescript.go. The default output is identical to before, and the existing golden files still match.-l jsalready emits JSDoc string-union typedefs, not enums.--enum-style constwith a non-TypeScript-lis rejected (--enum-style is only supported for ts, not php), and so is an unknown value.Reproduction
Against Appwrite Cloud staging: a
tablesdbdatabaseaudit277with tableItemsand a required enum columnrunItemOrigin(template,carryover,adhoc), thenappwrite pull tables --force. BEFORE is the released 28.6.0 binary, AFTER is built from this branch. tsc istypescript@5.9.3, run astsc --noEmit --strict --erasableSyntaxOnly --skipLibCheck false --module nodenext.Before (
appwrite types -l ts ./before/appwrite.ts):After (
appwrite types -l ts --enum-style const ./after/appwrite.ts):tsc exits 0. It also exits 0 on a consumer that uses
ItemsRunItemOrigin.CARRYOVER, the literal'adhoc'anditem.runItemOriginasItemsRunItemOrigin.node run-after.tsprintscarryover.After, default destination (
appwrite types -l ts --enum-style const ./after→appwrite.d.ts):tsc exits 0.
generate:
appwrite generate -l typescript --server falsewith 28.6.0 fails tsc ontypes.ts(3,13): error TS1294. With--enum-style constfrom this branch, tsc exits 0 on all four generated files.Without the flag, the AFTER binary's
typesoutput is identical to 28.6.0's apart from the invocation recorded in the header comment.Tests
internal/typegen: new golden filests.const.appwrite.ts(source) andts.const.appwrite.d.ts(declaration). Also a check thatDeclarationalone leaves the existingts.loose.appwrite.d.tsbaseline unchanged.internal/generator: new golden fileserver.const.types.tsforgenerate --enum-style const.internal/cmd: the flag surface for both commands now includes--enum-style. Also covered: theenumdefault, rejection of unknown values, and rejection ofconstfor non-TypeScript languages.generateoutput built from them, pass tsc 5.9.3 under--strict --erasableSyntaxOnly.php example.php cli,gofmt -lclean,go build ./... && go vet ./... && go test ./...inexamples/cli, andcomposer refactor:checkall pass.Fixes appwrite/sdk-for-cli#277
Closes #2071