Skip to content

Instantly share code, notes, and snippets.

@carlrip
Created December 17, 2019 18:22
Show Gist options
  • Save carlrip/82e9cde9a46426f7038e14e8353fbc71 to your computer and use it in GitHub Desktop.
Save carlrip/82e9cde9a46426f7038e14e8353fbc71 to your computer and use it in GitHub Desktop.
const assertions - array
type Person = {
id: number;
name: string;
scores: number[];
}
const people: Person[] = [
{ id: 1, name: "Bob", scores: [50, 45] },
{ id: 2, name: "Jane", scores: [70, 60] },
{ id: 3, name: "Paul", scores: [40, 75] }
]
function getPersonScores(id: number) {
const person = people.filter(person => person.id === id)[0];
return <const>[...person.scores];
}
const scores = getPersonScores(1); // `scores` is of type `readonly number[]`
scores.push(50); // 💥 - Type error - Property 'push' does not exist on type 'readonly number[]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment