Forbidden non-null assertion
116
117 event.preventDefault();
118 handleValueRemove(
119 multiSelectValues[multiSelectValues.length - 1]!,120 );
121 }}
122 />
Forbidden non-null assertion
58 const values = multiSelectValues.map((item) => (
59 <IntegrationPill
60 key={item}
61 option={data.find((i) => i.id === item)!} 62 onRemove={() => handleValueRemove(item)}
63 />
64 ));
Forbidden non-null assertion
8
9export const zodUnionFromArray = <T extends z.ZodTypeAny>(
10 array: CouldBeReadonlyArray<T>,
11) => z.union([array[0]!, array[1]!, ...array.slice(2)]);
Forbidden non-null assertion
8
9export const zodUnionFromArray = <T extends z.ZodTypeAny>(
10 array: CouldBeReadonlyArray<T>,
11) => z.union([array[0]!, array[1]!, ...array.slice(2)]);
Forbidden non-null assertion
4
5export const zodEnumFromArray = <T extends string>(
6 array: CouldBeReadonlyArray<T>,
7) => z.enum([array[0]!, ...array.slice(1)]); 8
9export const zodUnionFromArray = <T extends z.ZodTypeAny>(
10 array: CouldBeReadonlyArray<T>,
Forbidden non-null assertion
6export default {
7 schema: "./schema",
8 driver: "better-sqlite",
9 dbCredentials: { url: process.env.DB_URL! },10} satisfies Config;
Forbidden non-null assertion
26
27 return (
28 <Avatar {...commonProps}>
29 {currentSession.user.name!.substring(0, 2).toUpperCase()}30 </Avatar>
31 );
32};
Forbidden non-null assertion
20 <Avatar
21 {...commonProps}
22 src={currentSession.user.image}
23 alt={currentSession.user.name!}24 />
25 );
26
Forbidden non-null assertion
91 xOffset: addedNode.x!,
92 yOffset: addedNode.y!,
93 width: addedNode.w!,
94 height: addedNode.h!, 95 });
96 },
97 [moveItemToSection, section.id],
Forbidden non-null assertion
90 sectionId: section.id,
91 xOffset: addedNode.x!,
92 yOffset: addedNode.y!,
93 width: addedNode.w!, 94 height: addedNode.h!,
95 });
96 },
Forbidden non-null assertion
89 itemId,
90 sectionId: section.id,
91 xOffset: addedNode.x!,
92 yOffset: addedNode.y!, 93 width: addedNode.w!,
94 height: addedNode.h!,
95 });
Forbidden non-null assertion
88 moveItemToSection({
89 itemId,
90 sectionId: section.id,
91 xOffset: addedNode.x!, 92 yOffset: addedNode.y!,
93 width: addedNode.w!,
94 height: addedNode.h!,
Forbidden non-null assertion
74 xOffset: changedNode.x!,
75 yOffset: changedNode.y!,
76 width: changedNode.w!,
77 height: changedNode.h!, 78 });
79 },
80 [moveAndResizeItem],
Forbidden non-null assertion
73 itemId,
74 xOffset: changedNode.x!,
75 yOffset: changedNode.y!,
76 width: changedNode.w!, 77 height: changedNode.h!,
78 });
79 },
Forbidden non-null assertion
72 moveAndResizeItem({
73 itemId,
74 xOffset: changedNode.x!,
75 yOffset: changedNode.y!, 76 width: changedNode.w!,
77 height: changedNode.h!,
78 });
Forbidden non-null assertion
71 // Updates the react-query state
72 moveAndResizeItem({
73 itemId,
74 xOffset: changedNode.x!, 75 yOffset: changedNode.y!,
76 width: changedNode.w!,
77 height: changedNode.h!,
Forbidden non-null assertion
47 <Comp
48 options={state.options as never}
49 integrations={state.integrations.map(
50 (id) => integrationData.find((x) => x.id === id)!,51 )}
52 />
53 <Affix bottom={12} right={72}>
Forbidden non-null assertion
20 searchParams,
21}: NewIntegrationPageProps) {
22 const result = z
23 .enum([integrationKinds[0]!, ...integrationKinds.slice(1)])24 .safeParse(searchParams.kind);
25 if (!result.success) {
26 notFound();
Forbidden non-null assertion
129 onConfirm: () => {
130 form.setFieldValue(
131 `secrets.${index}.value`,
132 secretsMap.get(kind)!.value ?? "",133 );
134 res(true);
135 },
Forbidden non-null assertion
112 {secretsKinds.map((kind, index) => (
113 <SecretCard
114 key={kind}
115 secret={secretsMap.get(kind)!}116 onCancel={() =>
117 new Promise((res) => {
118 // When nothing changed, just close the secret card
Description
Using non-null assertions cancels out the benefits of strict null-checking, and introduces the possibility of runtime errors. Avoid non-null assertions unless absolutely necessary. If you still need to use one, write a skipcq comment to explain why it is safe.
Ideally, you want to have a validation function that confirms a value isn't null, with a return type like this:
type AccentedColor = `${Color}-${Accent}`
function isColorValid(name: string): name is AccentedColor {
// ...
}
Bad Practice
// a user named "injuly" may not exist in the DB
const injuly: User | null = db.getUserByName("injuly");
// Using the non-null assertion operator will bypass null-checking
const pfp = injuly!.profilePicture;
Recommended
const injuly: User | null = db.getUserByName("injuly");
const pfp = injuly?.profilePicture; // pfp: Image | undefined
// OR:
const pfp_ = injuly ? injuly.pfp : defaultPfp; // pfp: Image
Alternatively:
function isUserValid(userObj: User | null | undefined ): userObj is User {
return Boolean(userObj) && validate(userObj);
}
const injuly = db.getUserByName("injuly")
if (isUserValid(injuly)) {
const pfp = injuly.profilePicture;
// ...
}