gajus / gajus/flow-runtime

Feature request: opt-in Assert<T> type for arguments

Open
#222 0 comments 3 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
797
Forks
49
PR merge metrics
No merged PRs in 30d

Description

This is a:

- [ ] Bug Report
- [x] Feature Request
- [ ] Question
- [ ] Other

Which concerns:

- [ ] flow-runtime
- [x] babel-plugin-flow-runtime
- [ ] flow-runtime-validators
- [ ] flow-runtime-mobx
- [ ] flow-config-parser
- [ ] The documentation website

---

This is a PR I'd like to work on someday after fixing more important issues.

### What is the current behaviour?

I typically use `babel-plugin-flow-runtime` with `"optInOnly": true` for API argument validation. The code looks like the following:

```js
// @flow
// @flow-runtime enable

import reify from 'flow-runtime'
import type {Type} from 'flow-runtime'

type InitUserAttributes = {
email: string,
password: string,
}
type InitUserAttributesType = (reify: Type)

export function createUser(attributes: InitUserAttributes) {
InitUserAttributesType.assert(attributes)
...
}
```
---

### What is the expected behaviour?

It would be nice if I could shorten this to
```js
// @flow
// @flow-runtime enable

import type {Assert} from 'flow-runtime'

type InitUserAttributes = {
email: string,
password: string,
}

export function createUser(attributes: Assert) {
...
}
```

Basically `type Assert = T`, but `babel-plugin-flow-runtime` would insert runtime assertions for any arguments using the `Assert` type.

### Which package versions are you using?

`babel-plugin-flow-runtime`: `0.18.0`

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing babel-plugin-flow-runtime's handling of function argument annotations and its existing optInOnly behavior. Compare that path with the proposed Assert type syntax and define how runtime assertions should be generated while preserving the type alias behavior; done means the example can use Assert and validates arguments at runtime.

Written by the indexing model from the issue text.

Assessment

Tech stack
babel, javascript
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.