107 if (!globalTz && Array.isArray(formData) && formData.length) {
108 for (const { tz } of formData) {
109 if (tzMap.has(tz)) {
110 tzMap.set(tz, tzMap.get(tz)! + 1)111 } else {
112 tzMap.set(tz, 1)
113 }
1import { type Context, type ListrTask } from '~db/lib/generateData'
2
3const camelize = (s: string) => s.replace(/-./g, (x) => x[1]!.toUpperCase()) 4
5export const generateNamespaces = async (ctx: Context, task: ListrTask) => {
6 const { prisma, writeOutput } = ctx
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 {
// ...
}
// 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;
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;
// ...
}