Closed
Description
TypeScript Version: 2.9.2
Search Terms: string never undefined jQuery
Code
declare const childSelector: {}|undefined;
declare const elem: HTMLElement;
if (typeof childSelector === 'string') {
// childSelector is never (bug?)
const childElement: HTMLElement|null = elem.querySelector(childSelector);
// childElement is now null
if (!childElement) {
throw new Error('...');
}
// childElement is now never
childElement.addEventListener('click', () => 1);
}
Expected behavior:
TypeScript understands that childSelector
must be string
in the block. This is what TS 2.8 did.
Actual behavior:
TypeScript infers childSelector
to be never, then outsmarts the user's type annotation of HTMLElement|null
to be just null
, then deducts that in the last line, it must be never.