Skip to content

Instantly share code, notes, and snippets.

@andineck

andineck/map.ts

Created Apr 10, 2021
Embed
What would you like to do?
typescript
interface Option {
key: string;
label: string;
}
const results = [
{ k: "1", l: 1 },
{ k: "2", l: 2 },
];
// nachteil, dass beim mappen selber man keine typsicherheit hat
const mappedResult0 = results.map((r) => ({
key: r.k,
label: r.l.toString(),
}) as Option);
// schöne lösung
const mappedResult1 = results.map<Option>((r) => ({
key: r.k,
label: r.l.toString(),
}));
// auch ok
const mappedResult2 = results.map((r) => <Option>({
key: r.k,
label: r.l.toString(),
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment