TypeScript-DOM-lib-generator icon indicating copy to clipboard operation
TypeScript-DOM-lib-generator copied to clipboard

Add `__brand` to make empty interfaces nominal

Open saschanaz opened this issue 5 months ago • 6 comments

Because who have thought that there are so many existing empty interfaces which are also used as parameter types. And thus this follows the pattern in https://www.typescriptlang.org/play/?#example/nominal-typing.

Preparing for #2074.

saschanaz avatar Jul 19 '25 21:07 saschanaz

Thanks for the PR!

This section of the codebase is owned by @saschanaz - if they write a comment saying "LGTM" then it will be merged.

github-actions[bot] avatar Jul 19 '25 21:07 github-actions[bot]

https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1246/files#r881972594 suggested emitting a class with a private member. While I'm all for emitting classes (yes please) but a private member would only raise the possibility to clash with other libraries. Introducing a public member would allow other libraries to match the brand.

saschanaz avatar Jul 19 '25 21:07 saschanaz

These will end up being in completions and so on (I see people do like " brand": any to derank them); is this a pattern taken from the existing trusted types definition or something?

I don't know that we actually use type branding in any of the existing lib.d.ts files, so this would be a new thing I think.

jakebailey avatar Jul 21 '25 17:07 jakebailey

These will end up being in completions and so on

This is only for empty interfaces so I think that part is less a problem, although putting a space is an interesting solution.

is this a pattern taken from the existing trusted types definition or something?

https://www.typescriptlang.org/play/?#example/nominal-typing

As you saw in #2074 the most popular one (I don't think there's any other comparable library?) uses private brand: true 😞

saschanaz avatar Jul 22 '25 06:07 saschanaz

One thing I think we're going to have to try is to port this code into the main TS repo and run the extended suite.

jakebailey avatar Jul 24 '25 18:07 jakebailey

Perhaps a better approach would be to implement https://webidl.spec.whatwg.org/#ref-for-dfn-class-string, ie. resurrect the spirit of #1762 et al.?

This could only really be a TS 6.0+ change (for all lib targets to include es6's well-known symbols) but it seems better than introducing ghost properties across the entire library.

Renegade334 avatar Aug 19 '25 21:08 Renegade334