9
10const DisplayPosts = ({ pages }: { pages: Pages }) => {
11 return (
12 <>{pages!.map(({ feed }) => feed.map((post: typeof feed[number]) => <PostCard key={post.id} post={post} />))}</>13 )
14}
15
55 <Popover.Dropdown p={0} sx={{ height: '222px', overflowY: 'auto' }}>
56 {searchResult?.searchResult?.length! > 0 ? (
57 searchResult?.searchResult?.map((user) => (
58 <Group p={'0.5rem'} key={user!.id} sx={{ borderBottom: '1px solid lightgray' }}>59 <AvatarName avatar={user?.avatar} name={user?.name} />
60 </Group>
61 ))
25 utils.setQueryData(['post.get-post', { postId }], {
26 post: {
27 ...snapshot!.post,
28 comments: [res.commentAdded, ...snapshot!.post.comments],29 },
30 })
31 return { snapshot }
24
25 utils.setQueryData(['post.get-post', { postId }], {
26 post: {
27 ...snapshot!.post,28 comments: [res.commentAdded, ...snapshot!.post.comments],
29 },
30 })
93 data: {
94 postId: post.id,
95 userId: ctx.session.user.id,
96 body: caption!, 97 },
98 })
99 }
Type assertion is explicitly telling the compiler that we want to treat the entity as a different type. However, if we add a type assertion for a variable which is automatically inferred with the same type already then it is unnecessary to define the type explicitly.
Using a type assertion that does not change the type of an expression is unnecessary and should be avoided for a cleaner code.
// Types of 'foo' and 'bar' are already inferred as 'number', explicit type assertion in unnecessary
let foo = 3 as number;
let bar = <number>3;
function foobar(x: number): number {
return x!; // Type of 'x' is 'number' and is not 'null' or 'undefined'. Non-null assertion is unnecessary
}
type NewType = number;
let baz = 3 as NewType
let qux = <NewType>3
const foo = [1, 2] as const; // Using 'as const' enforces the array to be readonly
function foobar(x: number | undefined): number {
return x!;
}