Skip to content

Instantly share code, notes, and snippets.

@andineck
Created April 10, 2021 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andineck/3c04adc08ecc62f52e1ca3a02986087e to your computer and use it in GitHub Desktop.
Save andineck/3c04adc08ecc62f52e1ca3a02986087e to your computer and use it in GitHub Desktop.
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