Last active
April 9, 2018 02:03
-
-
Save rahulcs/f018383aad790941f3bb1a414b4e4fad to your computer and use it in GitHub Desktop.
playing with adding types with typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Status = 'Unapproved' | 'Rejected' | 'Approved'; | |
enum statusEnum { | |
Unapproved = 'grey', | |
Rejected = 'red', | |
Approved = 'green' | |
} | |
interface Arr { | |
id: number; | |
status: Status; | |
} | |
const arr: Arr[] = [{ id: 1, status: 'Approved' }, { id: 2, status: 'Unapproved' }, { id: 3, status: 'Rejected' }]; | |
const colors = arr.map(({ status }) => { | |
return statusEnum[status]; | |
}) | |
alert(colors); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* @flow */ | |
type Status = 'Approved' | 'Unapproved' | 'Rejected'; | |
type StatusObj = { | |
id: number, | |
status: Status | |
} | |
const arr:StatusObj[] = [{ id: 1, status: 'Approved' }, { id: 2, status: 'Unapproved' }, { id: 3, status: 'Rejected' }]; | |
type Color = 'green' | 'red' | 'grey'; | |
type StatusColor = {[Status]: Color} | |
const statusColor: StatusColor = { | |
'Approved': 'green', | |
'Rejected': 'red', | |
'Unapproved': 'grey' | |
}; | |
const colors:Color[] = arr.map(({ status }) => { | |
return statusColor[status]; | |
}) | |
alert(colors); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arr = [{ id: 1, status: 'Approved' }, { id: 2, status: 'Unapproved' }, { id: 3, status: 'Rejected' }]; | |
const statusColor = { | |
'Approved': 'green', | |
'Rejected': 'red', | |
'Unapproved': 'grey' | |
}; | |
const colors = arr.map(({ status }) => { | |
return statusColor[status]; | |
}) | |
alert(colors); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Status = 'Unapproved' | 'Rejected' | 'Approved'; | |
type Color = 'grey' | 'red' | 'green'; | |
/* Mapped Types */ | |
type StatusColor = { [C in Status]: Color }; | |
let statusEnum: StatusColor = { | |
Unapproved: 'grey', | |
Rejected: 'red', | |
Approved: 'green' | |
}; | |
interface Arr { | |
id: number; | |
status: Status; | |
} | |
const arr: Arr[] = [{ id: 1, status: 'Approved' }, { id: 2, status: 'Unapproved' }, { id: 3, status: 'Rejected' }]; | |
const colors = arr.map(({ status }) => { | |
return statusEnum[status]; | |
}) | |
alert(colors); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment