Skip to content

Conversation

@brendandahl
Copy link
Collaborator

@brendandahl brendandahl commented Dec 18, 2025

TS treats optional interface properties differently if they're defined with someProp?: type or someProp: type|undefined. When creating a object literal to match the interface, the ? doesn't require the property name whereas type|undefined requires the property name (though an undefined value is allowed).

Fixes #25978

TS treats fields differently if they're defined with `?` or
`type|undefined`. In the case of `?` it doesn't require the field name
whereas `type|undefined` requires the field name (though undefined is
allowed).

Fixes emscripten-core#25978
Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what you mean by "doesn't require the field name"? Should the test include that case maybe?

@brendandahl
Copy link
Collaborator Author

interface FooOptional { 
    bar?: number | undefined
}
interface FooUndefined {
    bar: number | undefined
}

const foo: FooOptional = {} // OK
const foo: FooOptional = { bar: undefined } // OK
const foo: FooUndefined = {} // error, bar is required
const foo: FooUndefined = {bar: undefined} // OK

This new behavior is automatically tested since I added an optional field and I didn't have to change any of the other value_object tests in test/other/embind_tsgen_main.ts. Previously, they would have required the optionalInt field.

@sbc100
Copy link
Collaborator

sbc100 commented Dec 18, 2025

I see, so when you say "it doesn't require the field name" you mean when creating an instance from a object literal? Maybe you could update the description to be more clear about?

@brendandahl brendandahl changed the title [embind] Fix TS definition for optional value object fields. [embind] Fix TS definition for optional value object properties. Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeScript type for optional struct member should be optional (?)

2 participants