Dafny: with `exactOptionalPropertyTypes`, an optional field whose type is an inline object loses its `Option`
Maintainers usually reply within 1 day
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
- Domain
- backend-api-design, compilers
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from midspiral/LemmaScript
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
midspiral/LemmaScript#355 · 1 comment ·
Maintainers usually reply within 1 day
-
In a pure function body, a `const` declared inside an `if` branch stays in scope after the `if`, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
midspiral/LemmaScript#362 ·
Maintainers usually reply within 1 day
-
A `const` or `let` inside a bare `{ }` block stays in scope after the block, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 15/100
midspiral/LemmaScript#361 ·
Maintainers usually reply within 1 day
-
A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 18/100
midspiral/LemmaScript#360 ·
Maintainers usually reply within 1 day
-
An assignment used as an expression, such as `(x = 7);` or `const y = (x = 7)`, is extracted as an equality test, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 3/5 Half a day Newbie friendliness 30/100
midspiral/LemmaScript#356 · 1 reaction ·
Maintainers usually reply within 1 day
All issues in midspiral/LemmaScript
Similar issues
-
bug done
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neavo/LinguaGacha#1124 ·
Maintainers usually reply within 1 day
-
[fullsend] E2E: backstage-microsoft-auth — catalogQuery missing error handler for network errorsPossibly taken @fullsend-ai-coder claimed this today. Opene2e-failure ready-to-code
Difficulty 2/5 Under an hour Newbie friendliness 78/100
redhat-developer/rhdh-plugin-export-overlays#4821 · 1 comment ·
Maintainers usually reply within 1 day
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
brylie/mission-island#225 ·
Maintainers usually reply within 1 day
-
status/need-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
google-gemini/gemini-cli#30412 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day