Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ways to seal objects, even at instantiation? #1164

Closed
STRML opened this issue Dec 7, 2015 · 4 comments
Closed

Ways to seal objects, even at instantiation? #1164

STRML opened this issue Dec 7, 2015 · 4 comments

Comments

@STRML
Copy link
Contributor

@STRML STRML commented Dec 7, 2015

We use objects often as a way to pass many parameters to remoting functions.

We'd like to very strictly typecheck these objects, but it appears we're unable to properly seal the objects in the type definitions. For example, we want the following code to fail:

type GetTableParams = {table: string, optionalProperty?: number};
db.getTable = (params: GetTableParams): Promise<Array<Object>> {
  ...
}

// This should throw an error
db.getTable({table: 'foo', anInvalidProperty: 'bar'});

Is there a way to signify that an object type should have only the properties in the typedef and nothing more?

@STRML
Copy link
Contributor Author

@STRML STRML commented Dec 7, 2015

I found a workaround that properly throws errors for extra properties:

type GetTableParams = {
  table: string,
  optionalProperty?: number,
  // Mark any other properties as invalid.
  [key: string]: void
};

db.getTable = (params: GetTableParams): Promise<Array<Object>> {
  ...
}
// Throws 'string: This type is incompatible with void'
db.getTable({table: 'foo', anInvalidProperty: 'bar'});

@aldendaniels
Copy link

@aldendaniels aldendaniels commented Jan 10, 2016

Thanks for the workaround. Proper support within Flow for sealed object types would be a big win for flow IMO.

@STRML
Copy link
Contributor Author

@STRML STRML commented Aug 23, 2016

Can close this now that we have $Exact<obj> and its shorthand, {|foo: string|}.

@STRML STRML closed this Aug 23, 2016
@gcanti
Copy link

@gcanti gcanti commented Aug 24, 2016

Is there a difference between the built-in $Exact<T> and the custom $Exact2<T> = T & $Shape<T>;?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants