-
Notifications
You must be signed in to change notification settings - Fork 0
/
generics.ts
44 lines (29 loc) · 952 Bytes
/
generics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// S => State
// T => Type
// Key => Key
// V => Value
// S => Element
// represents for the function that it can use a S type, and you define what S is.
// When do you define the type? the first time you call the function. Like, you send
// a string, and the function will only allow strings.
function useState<S>() {
let state: S;
function getState() {
return state;
}
function setState(newState: S) {
state = newState;
}
return { getState, setState };
}
const newState = useState<string>();
// newState.setState(1234); error
newState.setState('foo');
console.log('new state', newState.getState());
// if you want to delimite the types
function useState2<S extends number | string = string>() {
let state: S;
}
// then, the call will define if its number or string. If you try to define as boolean, you can't.
// number | string = string says that string is the default
// its like (acceptable types) = string