32
loading...
This website collects cookies to deliver better user experience
type Movie = {
uuid: string,
title: string,
comments: Comment[]
}
type Comment = {
uuid: string,
content: string,
}
const Comments = (movie: Movie) => {
if (movie?.comments?.length > 0) {
return movie.comments.map(comment =>
<p>comment?.content</p>)
} else {
return "No comments"
}
}
type Movie = {
uuid: string,
title: string,
comments?: Comment[] // optional property
}
type Comment = {
uuid: string,
content: string,
}
comments
is now optional property, and TS will now check if we do the check before we use this property as an array. Now after the type change we can follow the type doing code changes. In that way, types always define the contract and code follows them.unknown
to our wanted type.