Skip to content

Commit b4d4adb

Browse files
docs(website): improve typescript docs for operators
1 parent 1f53b60 commit b4d4adb

File tree

12 files changed

+303
-72
lines changed

12 files changed

+303
-72
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const javascript = {
2+
react: [
3+
{
4+
fileName: 'components/MyComponent.jsx',
5+
target: 'jsx',
6+
code: `
7+
import React from 'react'
8+
import { connect } from '../overmind'
9+
10+
const MyComponent = ({ overmind }) => (
11+
<button onClick={overmind.actions.functionalAction}>
12+
Test
13+
</button>
14+
)
15+
16+
export default connect(MyComponent)
17+
`,
18+
},
19+
],
20+
vue: [
21+
{
22+
fileName: 'components/MyComponent.vue (template)',
23+
target: 'markup',
24+
code: `
25+
<button on:click="overmind.actions.functionalAction()">
26+
Test
27+
</button>
28+
`,
29+
},
30+
{
31+
fileName: 'components/MyComponent.vue (script)',
32+
code: `
33+
import { connect } from '../overmind'
34+
35+
export default connect({})
36+
`,
37+
},
38+
],
39+
}
40+
41+
const typescript = {
42+
react: [
43+
{
44+
fileName: 'components/MyComponent.tsx',
45+
code: `
46+
import * as React from 'react'
47+
import { connect, Connect } from '../overmind'
48+
49+
type Props = Connect
50+
51+
const MyComponent: React.FunctionComponent<Props> = ({ overmind }) => (
52+
<button onClick={overmind.actions.functionalAction}>
53+
Test
54+
</button>
55+
)
56+
57+
export default connect(MyComponent)
58+
`,
59+
},
60+
],
61+
vue: [
62+
{
63+
fileName: 'components/MyComponent.vue (template)',
64+
target: 'markup',
65+
code: `
66+
<button on:click="overmind.actions.functionalAction()">
67+
Test
68+
</button>
69+
`,
70+
},
71+
{
72+
fileName: 'components/MyComponent.vue (script)',
73+
code: `
74+
import { connect } from '../overmind'
75+
76+
export default connect({})
77+
`,
78+
},
79+
],
80+
angular: [
81+
{
82+
fileName: 'components/grabdata.component.ts',
83+
code: `
84+
import { Component,Input } from '@angular/core';
85+
import { connect } from '../overmind'
86+
87+
@Component({
88+
selector: 'grab-data',
89+
template: \`
90+
<button (click)="overmind.actions.functionalAction()">
91+
Test
92+
</button>
93+
\`
94+
})
95+
@connect()
96+
export class GrabData {}
97+
`,
98+
},
99+
],
100+
}
101+
102+
export default (ts, view) => (ts ? typescript[view] : javascript[view])
Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
export default (ts) =>
22
ts
33
? [
4-
{
5-
fileName: 'overmind/operators.ts',
6-
code: `
7-
import { Operator, map, filter } from 'overmind'
8-
9-
const getEventTargetValue: Operator<Event, string> =
10-
map(({ value }) => value.currentTarget.value)
11-
12-
const lengthGreaterThan: (length: number) => Operator<string> =
13-
(length) => filter(({ value }) => value.length > length)
14-
15-
`,
16-
},
174
{
185
fileName: 'overmind/actions.ts',
196
code: `
207
import { Operator, pipe, debounce, action } from 'overmind'
8+
import { getEventTargetValue, lengthGreaterThan } from './operators'
219
2210
export const search: Operator<Event> = pipe(
2311
getEventTargetValue,
@@ -31,18 +19,21 @@ export const search: Operator<Event> = pipe(
3119
)
3220
`,
3321
},
34-
]
35-
: [
3622
{
37-
fileName: 'overmind/operators.js',
23+
fileName: 'overmind/operators.ts',
3824
code: `
39-
import { map, filter } from 'overmind'
25+
import { Operator, map, filter } from 'overmind'
4026
41-
export const getEventTargetValue = map(({ value }) => value.currentTarget.value)
27+
const getEventTargetValue: Operator<Event, string> =
28+
map(({ value }) => value.currentTarget.value)
4229
43-
export const lengthGreaterThan = (length) => filter(({ value }) => value.length > length)
30+
const lengthGreaterThan: (length: number) => Operator<string> =
31+
(length) => filter(({ value }) => value.length > length)
32+
4433
`,
4534
},
35+
]
36+
: [
4637
{
4738
fileName: 'overmind/actions.js',
4839
code: `
@@ -61,4 +52,14 @@ export const search = pipe(
6152
)
6253
`,
6354
},
55+
{
56+
fileName: 'overmind/operators.js',
57+
code: `
58+
import { map, filter } from 'overmind'
59+
60+
export const getEventTargetValue = map(({ value }) => value.currentTarget.value)
61+
62+
export const lengthGreaterThan = (length) => filter(({ value }) => value.length > length)
63+
`,
64+
},
6465
]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default (ts) =>
2+
ts
3+
? [
4+
{
5+
fileName: 'overmind/operators.ts',
6+
code: `
7+
import { Operator, filter } from 'overmind'
8+
9+
const lengthGreaterThan: (length: number) => Operator<string> =
10+
(length) => filter(({ value }) => value.length > length)
11+
12+
`,
13+
},
14+
]
15+
: [
16+
{
17+
fileName: 'overmind/operators.js',
18+
code: `
19+
import { map, filter } from 'overmind'
20+
21+
export const lengthGreaterThan = (length) => filter(({ value }) => value.length > length)
22+
`,
23+
},
24+
]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default () => [
2+
{
3+
code: `
4+
import { Action } from 'overmind'
5+
6+
export const noArgAction: Action = ({ value }) => {
7+
value // this becomes "void"
8+
}
9+
10+
export const argAction: Action<string> = ({ value }) => {
11+
value // this becomes "string"
12+
}
13+
`,
14+
},
15+
]

packages/overmind-website/examples/guide/goingfunctional/operatorinfer.ts renamed to packages/overmind-website/examples/guide/typescript/operatorinfer.ts

File renamed without changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default () => [
2+
{
3+
fileName: 'overmind/operators.ts',
4+
code: `
5+
import { Operator, action } from 'overmind'
6+
7+
export const doSomeStateChange: <T>() => Operator<T> =
8+
() => action(({ state }) => {
9+
state.foo = 'bar'
10+
})
11+
`,
12+
},
13+
{
14+
fileName: 'overmind/actions.ts',
15+
code: `
16+
import { Operator, pipe, action } from 'overmind'
17+
import { doSomeStateChange } from './operators'
18+
19+
export const setInput: Operator<string> = pipe(
20+
doSomeStateChange<string>(),
21+
action(({ value: input, state }) => {
22+
state.input = input
23+
})
24+
)
25+
`,
26+
},
27+
]

packages/overmind-website/examples/guide/goingfunctional/operatorinputsandoutputs.ts renamed to packages/overmind-website/examples/guide/typescript/operatorinputsandoutputs.ts

File renamed without changes.

packages/overmind-website/examples/guide/goingfunctional/operatorpartial.ts renamed to packages/overmind-website/examples/guide/typescript/operatorpartial.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ export default () => [
44
code: `
55
import { Operator, filter } from 'overmind'
66
7-
export const filterAwesomeUser: Operator<{ isAwesome: boolean }> =
7+
export const filterAwesome: Operator<{ isAwesome: boolean }> =
88
filter(({ value: somethingAwesome }) => somethingAwesome.isAwesome)
99
`,
1010
},
1111
{
1212
fileName: 'overmind/actions.ts',
1313
code: `
1414
import { Operator, pipe, action } from 'overmind'
15-
import { filterAwesomeUser } from './operators'
15+
import { filterAwesome } from './operators'
1616
import { User } from './state'
1717
1818
export const clickedUser: Operator<User> = pipe(
19-
filterAwesomeUser,
19+
// We get an error here, because this operator explicitly
20+
// outputs the type { isAwesome: boolean }
21+
filterAwesome,
2022
action(({ value: user, state }) => {
2123
state.awesomeUsersClickedCount++
2224
})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default () => [
2+
{
3+
fileName: 'overmind/operators.ts',
4+
code: `
5+
import { Operator, filter } from 'overmind'
6+
7+
export const filterAwesome: <T>() => Operator<{ isAwesome: boolean }, T> =
8+
() => filter(({ value: somethingAwesome }) => somethingAwesome.isAwesome)
9+
`,
10+
},
11+
{
12+
fileName: 'overmind/actions.ts',
13+
code: `
14+
import { Operator, pipe, action } from 'overmind'
15+
import { filterAwesome } from './operators'
16+
import { User } from './state'
17+
18+
export const clickedUser: Operator<User> = pipe(
19+
filterAwesome<User>(),
20+
action(({ value: user, state }) => {
21+
state.awesomeUsersClickedCount++
22+
})
23+
)
24+
`,
25+
},
26+
]

packages/overmind-website/examples/guide/goingfunctional/wrongoperator.ts renamed to packages/overmind-website/examples/guide/typescript/wrongoperator.ts

File renamed without changes.

0 commit comments

Comments
 (0)