IssueRaiser

Exact Types

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. ```ts interface User { username: string email: string } const user1: User = { username: 'x', email: 'y', foo: 'z' } //=> Currently errors when `foo` is unknown. const user2: 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. const user3: User = Object.assign({ username: 'x' }, { email: 'y', foo: 'z' }) //=> Does not currently error. const user4: 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. ```ts t

Posted by blakeembrey on Dec 15, 2016, 4:52 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments

support (Other)

Exact Types

blakeembreyDec 15, 2016, 4:52 AM

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. ```ts interface User { username: string email: string } const user1: User = { username: 'x', email: 'y', foo: 'z' } //=> Currently errors when `foo` is unknown. const user2: 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. const user3: User = Object.assign({ username: 'x' }, { email: 'y', foo: 'z' }) //=> Does not currently error. const user4: 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. ```ts t

Comments (0)

No comments yet. Start the discussion.

Comment on this issue