Skip to content

fix(typescript): optional scalar fields lose their optional modifier #194

@Shubh-Raj

Description

@Shubh-Raj

Description

When generating TypeScript code from a Concerto model, optional fields that use a scalar type lose their optional modifier (?).

Steps to Reproduce

Create a model with an optional scalar field and generate TypeScript:

Model: namespace org.example@1.0.0 scalar MyString extends String concept Person { o MyString nickname optional }

Command: concerto generate --model model.cto --target typescript

Expected vs Actual

Expected: nickname?: string;
Actual: nickname: string; (missing ?)

Root Cause

In lib/codegen/fromcto/typescript/typescriptvisitor.js, the visit() method calls this.visitField(thing.getScalarField(), parameters) for scalars. The scalar field doesn't inherit the parent's optional property.

Proposed Fix

Pass forceOptional via parameters instead of mutating the scalar field, then honor it in visitField().

Related

  • This bug was discovered via a TODO comment in accordproject/concerto at packages/concertino/scripts/generateTypes.js:119 which implemented a local workaround. Once this fix is merged, that workaround can be removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions