42 normal: () => (docs.switchToMode('normal').isInMotion = false),
43 visual: () =>
44 ((
45 docs.switchToMode('normal').pressKey(keys['ArrowLeft']) as typeof docs 46 ).isInMotion = false),
47 insert: () => (docs.switchToMode('normal').isInMotion = false),
48 visualLine: () => (docs.switchToMode('normal').isInMotion = false)
174 clickingMenuItem: boolean = false,
175 addNewLine = false
176 ) {
177 const elSelector = document.querySelector(178 `[${type}="${selector}"]`179 ) as HTMLElement | null;180 if (!elSelector) return;
181
182 if (!clickingMenuItem)
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.
// 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
const foo = [1, 2] as const; // Using 'as const' enforces the array to be readonly
function foobar(x: number | undefined): number {
return x!;
}