Dafny: with `exactOptionalPropertyTypes`, an optional field whose type is an inline object loses its `Option`

Open
#311 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@namin is already working on this.

Since Nov 18, 2026.

  • #318 by @namin — open

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
56/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

Start by tracing how the Dafny backend handles optional properties with inline object types, using the supplied Api example and generated Dafny output to find where the property's ? is lost under exactOptionalPropertyTypes. Compare that path with the named-interface and non-exact-optional cases. Done means the optional field is represented as Option<ApiFallback>, absence checks and record construction typecheck, and the example verifies.

Written by the indexing model from the issue text.

Description

Version: lsc 0.6.10 (npm), Dafny 4.11.

With "exactOptionalPropertyTypes": true in the tsconfig, a field like fallback?: { value?: string } is emitted as a plain fallback: ApiFallback instead of fallback: Option<ApiFallback>. The inner value? stays Option<string>. The same field with a named interface type (fallback?: Fallback) keeps its Option, and so does the inline one without exactOptionalPropertyTypes.

tsconfig:

{ "compilerOptions": { "strict": true, "exactOptionalPropertyTypes": true, "target": "ES2022", "module": "esnext", "moduleResolution": "bundler", "noEmit": true, "allowImportingTsExtensions": true, "skipLibCheck": true } }
//@ backend dafny
export interface Api { readonly name: string; readonly fallback?: { readonly value?: string } }

export function hasFallback(api: Api): boolean {
  //@ verify
  //@ ensures \result === (api.fallback !== undefined)
  return api.fallback !== undefined
}

export function noFallback(name: string): Api {
  //@ verify
  //@ ensures \result.fallback === undefined
  return { name }
}

lsc check --backend=dafny i3.ts emits:

datatype ApiFallback = ApiFallback(value: Option<string>)

datatype Api = Api(name: string, fallback: ApiFallback)

function hasFallback(api: Api): bool
{
  (api.fallback != None)
}

function noFallback(name: string): Api
{
  ApiFallback(None)
}

and fails:

i3.dfy(11,16): Error: arguments must have comparable types (got ApiFallback and Option<?>)
i3.dfy(15,45): Error: arguments must have comparable types (got ApiFallback and Option<?>)
i3.dfy(19,9): Error: Function body type mismatch (expected Api, got ApiFallback)
i3.dfy(25,37): Error: arguments must have comparable types (got ApiFallback and Option<?>)

Every absence test is still emitted as == None, so the model fails to typecheck rather than proving something false; I did not find a shape where it verified wrongly. Building the record without the field is also emitted wrongly: noFallback becomes ApiFallback(None), the inner type's constructor, instead of Api(name, None). In a larger file we also saw /* missing: fallback */ emitted in the constructor call.

Without exactOptionalPropertyTypes (only strict), the same file gives datatype Api = Api(name: string, fallback: Option<ApiFallback>) and Api(name, None), and verifies (2 verified, 0 errors).

Under exactOptionalPropertyTypes the declared type of fallback is { value?: string }, not { value?: string } | undefined. So it looks as if optionality is read off the type (which no longer holds undefined) rather than off the property's ? token, but only on the inline-object path, since a named interface type is fine.

Suggested fix: decide optionality from the property declaration's questionToken (or SymbolFlags.Optional), not from whether its type includes undefined. exactOptionalPropertyTypes changes the second and not the first.

Workaround: name the inline type (interface Fallback { value?: string }, fallback?: Fallback).

Related, separate: two structurally identical inline types get two unrelated datatypes. interface Api { fallback?: { value?: string } } and interface Out { fallback?: { value?: string } } give ApiFallback and OutFallback, so return { name: api.name, fallback: api.fallback } for an Out fails with expected OutFallback, found ApiFallback. In TypeScript the two are the same type. Naming the type works around this too.

Dominant language
TypeScript
Stars
113
Forks
13
Avg merge
15h 35m
Merged PRs (30d)
56

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from midspiral/LemmaScript

All issues in midspiral/LemmaScript

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.