Skip to content

Commit fd31a97

Browse files
authored
Remove unnecessary type assertions (#599)
* Remove unnecessary type assertions This is the result of turning on the no-unnecessary-type-assertion tslint pass and running `--fix`. There was one place where I disabled this pass where we needed `!` to note that a variable was definitely assigned to before it was read. I like this lint pass because it makes the code a bit easier to read and understand. I will admit though that I'm proposing this change because we use this lint pass downstream, and it's annoying seeing the lint warnings.
1 parent eaebc4f commit fd31a97

File tree

6 files changed

+17
-16
lines changed

6 files changed

+17
-16
lines changed

src/lib/decorators.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ const standardCustomElement =
7171
export const customElement = (tagName: string) =>
7272
(classOrDescriptor: Constructor<HTMLElement>|ClassDescriptor) =>
7373
(typeof classOrDescriptor === 'function') ?
74-
legacyCustomElement(
75-
tagName, classOrDescriptor as Constructor<HTMLElement>) :
76-
standardCustomElement(tagName, classOrDescriptor as ClassDescriptor);
74+
legacyCustomElement(tagName, classOrDescriptor) :
75+
standardCustomElement(tagName, classOrDescriptor);
7776

7877
const standardProperty =
7978
(options: PropertyDeclaration, element: ClassElement) => {
@@ -109,7 +108,7 @@ const standardProperty =
109108
// tslint:disable-next-line:no-any decorator
110109
initializer(this: any) {
111110
if (typeof element.initializer === 'function') {
112-
this[element.key] = element.initializer!.call(this);
111+
this[element.key] = element.initializer.call(this);
113112
}
114113
},
115114
finisher(clazz: typeof UpdatingElement) {
@@ -122,7 +121,7 @@ const standardProperty =
122121
const legacyProperty =
123122
(options: PropertyDeclaration, proto: Object, name: PropertyKey) => {
124123
(proto.constructor as typeof UpdatingElement)
125-
.createProperty(name!, options);
124+
.createProperty(name, options);
126125
};
127126

128127
/**

src/lib/updating-element.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ export abstract class UpdatingElement extends HTMLElement {
296296
Object.defineProperty(this.prototype, name, {
297297
// tslint:disable-next-line:no-any no symbol in index
298298
get(): any {
299-
// tslint:disable-next-line:no-any no symbol in index
300-
return (this as any)[key];
299+
return this[key];
301300
},
302301
set(this: UpdatingElement, value: unknown) {
303302
// tslint:disable-next-line:no-any no symbol in index
@@ -632,6 +631,8 @@ export abstract class UpdatingElement extends HTMLElement {
632631
typeof (result as PromiseLike<unknown>).then === 'function') {
633632
await result;
634633
}
634+
// TypeScript can't tell that we've initialized resolve.
635+
// tslint:disable-next-line:no-unnecessary-type-assertion
635636
resolve!(!this._hasRequestedUpdate);
636637
}
637638

src/lit-element.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class LitElement extends UpdatingElement {
117117
return set;
118118
}, new Set<CSSResult>());
119119
// Array.from does not work on Set in IE
120-
styleSet.forEach((v) => styles!.unshift(v));
120+
styleSet.forEach((v) => styles.unshift(v));
121121
} else if (userStyles) {
122122
styles.push(userStyles);
123123
}
@@ -214,8 +214,8 @@ export class LitElement extends UpdatingElement {
214214
(this.constructor as typeof LitElement)
215215
.render(
216216
templateResult,
217-
this.renderRoot!,
218-
{scopeName: this.localName!, eventContext: this});
217+
this.renderRoot,
218+
{scopeName: this.localName, eventContext: this});
219219
}
220220
// When native Shadow DOM is used but adoptedStyles are not supported,
221221
// insert styling after rendering to ensure adoptedStyles have highest
@@ -225,7 +225,7 @@ export class LitElement extends UpdatingElement {
225225
(this.constructor as typeof LitElement)._styles!.forEach((s) => {
226226
const style = document.createElement('style');
227227
style.textContent = s.cssText;
228-
this.renderRoot!.appendChild(style);
228+
this.renderRoot.appendChild(style);
229229
});
230230
}
231231
}

src/test/lib/decorators_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ suite('decorators', () => {
372372
const c = new C();
373373
container.appendChild(c);
374374
await c.updateComplete;
375-
const divs = c.divs!;
375+
const divs = c.divs;
376376
// This is not true in ShadyDOM:
377377
// assert.instanceOf(divs, NodeList);
378378
assert.lengthOf(divs, 2);

src/test/lit-element_styling_test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ suite('Styling', () => {
201201
div = inner!.shadowRoot!.querySelector('div');
202202
assert.equal(
203203
getComputedStyleValue(div!, 'border-top-width').trim(), '2px');
204-
el2!.shadowRoot!.appendChild(inner!);
204+
el2.shadowRoot!.appendChild(inner!);
205205

206206
// Workaround for Safari 9 Promise timing bugs.
207207
await el.updateComplete;
@@ -307,14 +307,14 @@ suite('Styling', () => {
307307

308308
// Workaround for Safari 9 Promise timing bugs.
309309
await firstApplied.updateComplete && el.updateComplete &&
310-
await (el.applied as I)!.updateComplete;
310+
await (el.applied as I).updateComplete;
311311

312312
await nextFrame();
313313
assert.equal(
314-
getComputedStyleValue(firstApplied!, 'border-top-width').trim(),
314+
getComputedStyleValue(firstApplied, 'border-top-width').trim(),
315315
'2px');
316316
assert.equal(
317-
getComputedStyleValue(firstApplied!, 'margin-top').trim(), '10px');
317+
getComputedStyleValue(firstApplied, 'margin-top').trim(), '10px');
318318
assert.equal(
319319
getComputedStyleValue(el.applied!, 'border-top-width').trim(),
320320
'10px');

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"no-internal-module": true,
1414
"no-trailing-whitespace": true,
1515
"no-var-keyword": true,
16+
"no-unnecessary-type-assertion": true,
1617
"one-line": [
1718
true,
1819
"check-open-brace",

0 commit comments

Comments
 (0)