You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a proposal to enable a syntax for exact types. A similar feature can be seen in Flow (https://flowtype.org/docs/objects.html#exact-object-types), but I would like to propose it as a feature used for type literals and not interfaces. The specific syntax I'd propose using is the pipe (which almost mirrors the Flow implementation, but it should surround the type statement), as it's familiar as the mathematical absolute syntax.
interfaceUser{username: stringemail: string}constuser1: User={username: 'x',email: 'y',foo: 'z'}//=> Currently errors when `foo` is unknown.constuser2: Exact<User>={username: 'x',email: 'y',foo: 'z'}//=> Still errors with `foo` unknown.// Primary use-case is when you're creating a new type from expressions and you'd like the// language to support you in ensuring no new properties are accidentally being added.// Especially useful when the assigned together types may come from other parts of the application // and the result may be stored somewhere where extra fields are not useful.constuser3: User=Object.assign({username: 'x'},{email: 'y',foo: 'z'})//=> Does not currently error.constuser4: Exact<User>=Object.assign({username: 'x'},{email: 'y',foo: 'z'})//=> Will error as `foo` is unknown.
This syntax change would be a new feature and affect new definition files being written if used as a parameter or exposed type. This syntax could be combined with other more complex types.
Apologies in advance if this is a duplicate, I could not seem to find the right keywords to find any duplicates of this feature.
Edit: This post was updated to use the preferred syntax proposal mentioned at #12936 (comment), which encompasses using a simpler syntax with a generic type to enable usage in expressions.
johnnyreilly, mohsen1, msemikin, gcanti, svieira and 1197 morehansott, bl-ue, qb-vajahath, dmitrijs-balcers, alexandreannic and 54 moremshwery, sobolevn, vajahath, devinrhode2, SPGoding and 95 morecaptain-yossarian, sonirico, sobolevn, vajahath, devinrhode2 and 62 moressalbdivad, Slashmsu, MichalKalita, MatthiasKunnen, nodkz and 72 more