Skip to content

Instantly share code, notes, and snippets.

@dp-singh
Last active January 18, 2018 04:04
Show Gist options
  • Save dp-singh/34649607a75db11030ffcccb2270d2ba to your computer and use it in GitHub Desktop.
Save dp-singh/34649607a75db11030ffcccb2270d2ba to your computer and use it in GitHub Desktop.
Typescript custom type
//JSON is way of communication between client and server
//custom type
type Task={
id: number;
title: string;
detail: string;
createdAt: Date;
updateAt: Date;
}
//declaration of array
var arr: Task[];
arr = [
{
id: 1234,
title: "Mr D",
detail: "Learning Array",
createdAt: new Date(),
updateAt: new Date()
},
{
id: 1235,
title: "Mr D",
detail: "Learning Array",
createdAt: new Date(),
updateAt: new Date()
},
{
id: 1236,
title: "Mr D",
detail: "Learning Array",
createdAt: new Date(),
updateAt: new Date()
}
]
/**
* array , id as an input
* return the corresponding Task object
*/
function findTask(arr: Task[], id: number): Task| undefined {
//confusing
//return tasks.find(task => task.id == id)
for (var index = 0; index < arr.length; index++) {
if (arr[index].id == id)
return arr[index];
}
return undefined;
}
console.log(findTask(arr,1236))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment