Skip to content

Instantly share code, notes, and snippets.

@rahulcs
Last active April 9, 2018 02:03
Show Gist options
  • Save rahulcs/f018383aad790941f3bb1a414b4e4fad to your computer and use it in GitHub Desktop.
Save rahulcs/f018383aad790941f3bb1a414b4e4fad to your computer and use it in GitHub Desktop.
playing with adding types with typescript
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);
/* @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);
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);
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