This assertion is unnecessary since it does not change the type of the expression
99 const m = tokens[idx].info.trim().match(/^details\s+(.*)$/);
100 if (tokens[idx].nesting === 1) {
101 return `<details class="custom-block details"><summary>${md.utils.escapeHtml(
102 m![1]103 )}</summary>\n`;
104 } else {
105 return "</details>\n";
This assertion is unnecessary since it does not change the type of the expression
86 const m = tokens[idx].info.trim().match(/^info\s+(.*)$/);
87 if (tokens[idx].nesting === 1) {
88 return `<div class="custom-block info"><p class="custom-block-title">${md.utils.escapeHtml(
89 m![1] 90 )}</p>\n`;
91 } else {
92 return "</div>\n";
This assertion is unnecessary since it does not change the type of the expression
73 const m = tokens[idx].info.trim().match(/^danger\s+(.*)$/);
74 if (tokens[idx].nesting === 1) {
75 return `<div class="custom-block danger"><p class="custom-block-title">${md.utils.escapeHtml(
76 m![1] 77 )}</p>\n`;
78 } else {
79 return "</div>\n";
This assertion is unnecessary since it does not change the type of the expression
60 const m = tokens[idx].info.trim().match(/^warning\s+(.*)$/);
61 if (tokens[idx].nesting === 1) {
62 return `<div class="custom-block warning"><p class="custom-block-title">${md.utils.escapeHtml(
63 m![1] 64 )}</p>\n`;
65 } else {
66 return "</div>\n";
This assertion is unnecessary since it does not change the type of the expression
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(
50 m![1] 51 )}</p>\n`;
52 } else {
53 return "</div>\n";
Description
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.
Bad Practice
// 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
Recommended
const foo = [1, 2] as const; // Using 'as const' enforces the array to be readonly
function foobar(x: number | undefined): number {
return x!;
}