facebook / facebook/flow

Feature Suggestion: Filtering types by literal property comparison

Open
#6,830 16 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Suppose I have the following types:
```javascript
type Test = {
testType: 'single'|'set'
}
type TestSet = Test & {
testType: 'set',
setOnlyProperty: string
}
type TestSingle = Test & {
testType: 'single',
singleOnlyProperty: string
}
```
It would be very useful to be able to say,
```javascript
var test: Test = ...;
if(testType === 'set') {
console.log(test.setOnlyProperty);
} else {
console.log(test.singleOnlyProperty);
}
```
I'd think this would be doable, but I'm not a type-system expert. It also occurs to me now that once the ES pattern-matching syntax comes out this kind of thing would be even more useful, and possibly easier to implement.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.