90 render: (tokens, idx) => {
91 const m = tokens[idx].info.trim().match(/^details\s+(.*)$/);
92 if (tokens[idx].nesting === 1) {
93 return `<details class="custom-block details"><summary>${md.utils.escapeHtml(m![1])}</summary>\n`; 94 } else {
95 return "</details>\n";
96 }
79 render: (tokens, idx) => {
80 const m = tokens[idx].info.trim().match(/^info\s+(.*)$/);
81 if (tokens[idx].nesting === 1) {
82 return `<div class="custom-block info"><p class="custom-block-title">${md.utils.escapeHtml(m![1])}</p>\n`; 83 } else {
84 return "</div>\n";
85 }
68 render: (tokens, idx) => {
69 const m = tokens[idx].info.trim().match(/^danger\s+(.*)$/);
70 if (tokens[idx].nesting === 1) {
71 return `<div class="custom-block danger"><p class="custom-block-title">${md.utils.escapeHtml(m![1])}</p>\n`; 72 } else {
73 return "</div>\n";
74 }
57 render: (tokens, idx) => {
58 const m = tokens[idx].info.trim().match(/^warning\s+(.*)$/);
59 if (tokens[idx].nesting === 1) {
60 return `<div class="custom-block warning"><p class="custom-block-title">${md.utils.escapeHtml(m![1])}</p>\n`; 61 } else {
62 return "</div>\n";
63 }
46 render: (tokens, idx) => {
47 const m = tokens[idx].info.trim().match(/^tip\s+(.*)$/);
48 if (tokens[idx].nesting === 1) {
49 return `<div class="custom-block tip"><p class="custom-block-title">${md.utils.escapeHtml(m![1])}</p>\n`; 50 } else {
51 return "</div>\n";
52 }
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;
// ...
}