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 out: "./migrations",
11} satisfies Config;
Forbidden non-null assertion
35 });
36 expect(result.user).toBeDefined();
37 expect(result.user!.id).toEqual(user.id);
38 expect(result.user!.name).toEqual(user.name); 39 });
40});
41
Forbidden non-null assertion
34 newSession: {},
35 });
36 expect(result.user).toBeDefined();
37 expect(result.user!.id).toEqual(user.id); 38 expect(result.user!.name).toEqual(user.name);
39 });
40});
Forbidden non-null assertion
41
42 await adapter.createSession({
43 sessionToken,
44 userId: user.id!,45 expires: sessionExpiry,
46 });
47
Forbidden non-null assertion
248 expect(apiKey.updatedAt).toEqual(fakeNow);
249 expect(username.value).not.toEqual(usernameToInsert.value);
250 expect(password.value).toEqual(passwordToInsert.value);
251 expect(apiKey.value).not.toEqual(input.secrets[2]!.value);252 });
253
254 it("should throw an error if the integration does not exist", async () => {
Forbidden non-null assertion
228 expect(dbIntegration).toBeDefined();
229 expect(dbIntegration!.name).toBe(input.name);
230 expect(dbIntegration!.kind).toBe(input.kind);
231 expect(dbIntegration!.url).toBe(input.url);232
233 expect(dbSecrets.length).toBe(3);
234 const username = expectToBeDefined(
Forbidden non-null assertion
227
228 expect(dbIntegration).toBeDefined();
229 expect(dbIntegration!.name).toBe(input.name);
230 expect(dbIntegration!.kind).toBe(input.kind);231 expect(dbIntegration!.url).toBe(input.url);
232
233 expect(dbSecrets.length).toBe(3);
Forbidden non-null assertion
226 const dbSecrets = await db.query.integrationSecrets.findMany();
227
228 expect(dbIntegration).toBeDefined();
229 expect(dbIntegration!.name).toBe(input.name);230 expect(dbIntegration!.kind).toBe(input.kind);
231 expect(dbIntegration!.url).toBe(input.url);
232
Forbidden non-null assertion
164 expect(dbSecret).toBeDefined();
165 expect(dbSecret!.kind).toBe(input.secrets[0]!.kind);
166 expect(dbSecret!.value).toMatch(/^[a-f0-9]+.[a-f0-9]+$/);
167 expect(dbSecret!.updatedAt).toEqual(fakeNow);168 });
169});
170
Forbidden non-null assertion
163 expect(dbSecret!.integrationId).toBe(dbIntegration!.id);
164 expect(dbSecret).toBeDefined();
165 expect(dbSecret!.kind).toBe(input.secrets[0]!.kind);
166 expect(dbSecret!.value).toMatch(/^[a-f0-9]+.[a-f0-9]+$/);167 expect(dbSecret!.updatedAt).toEqual(fakeNow);
168 });
169});
Forbidden non-null assertion
162
163 expect(dbSecret!.integrationId).toBe(dbIntegration!.id);
164 expect(dbSecret).toBeDefined();
165 expect(dbSecret!.kind).toBe(input.secrets[0]!.kind);166 expect(dbSecret!.value).toMatch(/^[a-f0-9]+.[a-f0-9]+$/);
167 expect(dbSecret!.updatedAt).toEqual(fakeNow);
168 });
Forbidden non-null assertion
162
163 expect(dbSecret!.integrationId).toBe(dbIntegration!.id);
164 expect(dbSecret).toBeDefined();
165 expect(dbSecret!.kind).toBe(input.secrets[0]!.kind);166 expect(dbSecret!.value).toMatch(/^[a-f0-9]+.[a-f0-9]+$/);
167 expect(dbSecret!.updatedAt).toEqual(fakeNow);
168 });
Forbidden non-null assertion
160 expect(dbIntegration!.kind).toBe(input.kind);
161 expect(dbIntegration!.url).toBe(input.url);
162
163 expect(dbSecret!.integrationId).toBe(dbIntegration!.id);164 expect(dbSecret).toBeDefined();
165 expect(dbSecret!.kind).toBe(input.secrets[0]!.kind);
166 expect(dbSecret!.value).toMatch(/^[a-f0-9]+.[a-f0-9]+$/);
Forbidden non-null assertion
160 expect(dbIntegration!.kind).toBe(input.kind);
161 expect(dbIntegration!.url).toBe(input.url);
162
163 expect(dbSecret!.integrationId).toBe(dbIntegration!.id);164 expect(dbSecret).toBeDefined();
165 expect(dbSecret!.kind).toBe(input.secrets[0]!.kind);
166 expect(dbSecret!.value).toMatch(/^[a-f0-9]+.[a-f0-9]+$/);
Forbidden non-null assertion
158 expect(dbIntegration).toBeDefined();
159 expect(dbIntegration!.name).toBe(input.name);
160 expect(dbIntegration!.kind).toBe(input.kind);
161 expect(dbIntegration!.url).toBe(input.url);162
163 expect(dbSecret!.integrationId).toBe(dbIntegration!.id);
164 expect(dbSecret).toBeDefined();
Forbidden non-null assertion
157 const dbSecret = await db.query.integrationSecrets.findFirst();
158 expect(dbIntegration).toBeDefined();
159 expect(dbIntegration!.name).toBe(input.name);
160 expect(dbIntegration!.kind).toBe(input.kind);161 expect(dbIntegration!.url).toBe(input.url);
162
163 expect(dbSecret!.integrationId).toBe(dbIntegration!.id);
Forbidden non-null assertion
156 const dbIntegration = await db.query.integrations.findFirst();
157 const dbSecret = await db.query.integrationSecrets.findFirst();
158 expect(dbIntegration).toBeDefined();
159 expect(dbIntegration!.name).toBe(input.name);160 expect(dbIntegration!.kind).toBe(input.kind);
161 expect(dbIntegration!.url).toBe(input.url);
162
Forbidden non-null assertion
38 const result = await caller.all();
39 expect(result.length).toBe(2);
40 expect(result[0]!.kind).toBe("plex");
41 expect(result[1]!.kind).toBe("homeAssistant"); 42 });
43});
44
Forbidden non-null assertion
37
38 const result = await caller.all();
39 expect(result.length).toBe(2);
40 expect(result[0]!.kind).toBe("plex"); 41 expect(result[1]!.kind).toBe("homeAssistant");
42 });
43});
Forbidden non-null assertion
26
27 return (
28 <Avatar {...commonProps}>
29 {currentSession.user.name!.substring(0, 2).toUpperCase()}30 </Avatar>
31 );
32};
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;
// ...
}