Created
December 17, 2019 18:22
-
-
Save carlrip/82e9cde9a46426f7038e14e8353fbc71 to your computer and use it in GitHub Desktop.
const assertions - array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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