Skip to content

Commit

Permalink
Typing fixes for TypeScript 4.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
aomarks committed Aug 31, 2021
1 parent da2272e commit d4596af
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/labs/ssr/src/lib/element-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type AttributesMap = Map<string, string>;
export const getElementRenderer = (
{elementRenderers}: RenderInfo,
tagName: string,
ceClass: typeof HTMLElement = customElements.get(tagName),
ceClass: typeof HTMLElement | undefined = customElements.get(tagName),
attributes: AttributesMap = new Map()
): ElementRenderer | undefined => {
if (ceClass === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion packages/labs/ssr/src/lib/lit-element-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class LitElementRenderer extends ElementRenderer {

constructor(tagName: string) {
super(tagName);
this.element = new (customElements.get(this.tagName))();
this.element = new (customElements.get(this.tagName)!)() as LitElement;
}

connectedCallback() {
Expand Down
16 changes: 10 additions & 6 deletions packages/lit-html/src/experimental-hydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,16 @@ export const hydrate = (
// templates
const stack: Array<ChildPartState> = [];

const walker = document.createTreeWalker(
container,
NodeFilter.SHOW_COMMENT,
null,
false
);
const walker = (
document.createTreeWalker as (
root: Node,
whatToShow: number,
filter: NodeFilter | null,
// All 4 parameters are required for IE11, but TypeScript 4.4 removed the
// entityReferenceExpansion parameter. This is the previous signature.
entityReferenceExpansion?: boolean
) => TreeWalker
)(container, NodeFilter.SHOW_COMMENT, null, false);
let marker: Comment | null;

// Walk the DOM looking for part marker comments
Expand Down
21 changes: 14 additions & 7 deletions packages/lit-html/src/lit-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,16 @@ if (ENABLE_EXTRA_SECURITY_HOOKS) {
}
}

const walker = d.createTreeWalker(
d,
129 /* NodeFilter.SHOW_{ELEMENT|COMMENT} */,
null,
false
);
const walker = (
d.createTreeWalker as (
root: Node,
whatToShow: number,
filter: NodeFilter | null,
// All 4 parameters are required for IE11, but TypeScript 4.4 removed the
// entityReferenceExpansion parameter. This is the previous signature.
entityReferenceExpansion?: boolean
) => TreeWalker
)(d, 129 /* NodeFilter.SHOW_{ELEMENT|COMMENT} */, null, false);

let sanitizerFactoryInternal: SanitizerFactory = noopSanitizer;

Expand Down Expand Up @@ -1068,7 +1072,10 @@ class ChildPart implements Disconnectable {
get parentNode(): Node {
let parentNode: Node = wrap(this._$startNode).parentNode!;
const parent = this._$parent;
if (parent !== undefined && parentNode.nodeType === 11 /* Node.DOCUMENT_FRAGMENT */) {
if (
parent !== undefined &&
parentNode.nodeType === 11 /* Node.DOCUMENT_FRAGMENT */
) {
// If the parentNode is a DocumentFragment, it may be because the DOM is
// still in the cloned fragment during initial render; if so, get the real
// parentNode the part will be committed into by asking the parent.
Expand Down
4 changes: 2 additions & 2 deletions packages/localize-tools/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ export async function runAndLog(argv: string[]): Promise<number> {
console.error(err.message);
} else {
console.error('Unexpected error\n');
console.error(err.message);
console.error((err as Error).message);
console.error();
console.error(err.stack);
console.error((err as Error).stack);
}
console.log();
console.log(`Version: ${await version()}`);
Expand Down
5 changes: 3 additions & 2 deletions packages/localize-tools/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function readConfigFileAndWriteSchema(configPath: string): Config {
str = fs.readFileSync(configPath, 'utf8');
} catch (e) {
throw new KnownError(
`Could not read config file from ${configPath}:\n${e.message}`
`Could not read config file from ${configPath}:\n` + (e as Error).message
);
}

Expand All @@ -32,7 +32,8 @@ export function readConfigFileAndWriteSchema(configPath: string): Config {
parsed = JSON.parse(str);
} catch (e) {
throw new KnownError(
`Invalid JSON found in config file ${configPath}:\n${e.message}`
`Invalid JSON found in config file ${configPath}:\n` +
(e as Error).message
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/localize-tools/src/formatters/xlb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class XlbFormatter implements Formatter {
throw new KnownError(
`Error creating XLB directory: ${parentDir}\n` +
`Do you have write permission?\n` +
e.message
(e as Error).message
);
}
try {
Expand All @@ -159,7 +159,7 @@ class XlbFormatter implements Formatter {
throw new KnownError(
`Error creating XLB file: ${filePath}\n` +
`Do you have write permission?\n` +
e.message
(e as Error).message
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/localize-tools/src/formatters/xliff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class XliffFormatter implements Formatter {
try {
xmlStr = fsExtra.readFileSync(path, 'utf8');
} catch (err) {
if (err.code === 'ENOENT') {
if ((err as Error & {code: string}).code === 'ENOENT') {
// It's ok if the file doesn't exist, it's probably just the first
// time we're running for this locale.
continue;
Expand Down Expand Up @@ -148,7 +148,7 @@ export class XliffFormatter implements Formatter {
throw new KnownError(
`Error creating XLIFF directory: ${xliffDir}\n` +
`Do you have write permission?\n` +
e.message
(e as Error).message
);
}
const writes: Array<Promise<void>> = [];
Expand All @@ -164,7 +164,7 @@ export class XliffFormatter implements Formatter {
throw new KnownError(
`Error creating XLIFF file: ${path}\n` +
`Do you have write permission?\n` +
e.message
(e as Error).message
);
})
);
Expand Down
4 changes: 2 additions & 2 deletions packages/localize-tools/src/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const allLocales = [
throw new KnownError(
`Error creating locales module directory: ${parentDir}\n` +
`Do you have write permission?\n` +
e.message
(e as Error).message
);
}
try {
Expand All @@ -77,7 +77,7 @@ export const allLocales = [
throw new KnownError(
`Error creating locales module file: ${filePath}\n` +
`Do you have write permission?\n` +
e.message
(e as Error).message
);
}
}
6 changes: 3 additions & 3 deletions packages/localize-tools/src/modes/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function runtimeOutput(
throw new KnownError(
`Error creating TypeScript locales directory: ${outputDir}\n` +
`Do you have write permission?\n` +
e.message
(e as Error).message
);
}
for (const locale of config.targetLocales) {
Expand All @@ -88,11 +88,11 @@ async function runtimeOutput(
);
const filename = pathLib.join(outputDir, `${locale}.ts`);
writes.push(
fsExtra.writeFile(filename, ts, 'utf8').catch((e) => {
fsExtra.writeFile(filename, ts, 'utf8').catch((e: unknown) => {
throw new KnownError(
`Error writing TypeScript file: ${filename}\n` +
`Do you have write permission?\n` +
e.message
(e as Error).message
);
})
);
Expand Down
2 changes: 1 addition & 1 deletion packages/localize/examples/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ window.addEventListener(LOCALE_STATUS_EVENT, ({detail}) => {
} catch (e) {
// Either the URL locale code was invalid, or there was a problem loading
// the locale module.
console.error(`Error loading locale: ${e.message}`);
console.error(`Error loading locale: ${(e as Error).message}`);
}
render(html` <x-greeter></x-greeter> `, main);
})();
2 changes: 1 addition & 1 deletion packages/localize/src/tests/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ suite('runtime localization configuration', () => {
error = e;
}
assert.isDefined(error);
assert.equal(error.message, 'Some error');
assert.equal((error as Error).message, 'Some error');

assertEventLogEqualsAndFlush([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ suite('@customElement', () => {
}
}
const DefinedC = customElements.get(tagName);
assert.strictEqual(DefinedC, C1);
assert.strictEqual(DefinedC, C1 as typeof DefinedC);
});
});

0 comments on commit d4596af

Please sign in to comment.