Description
I think I may have found an issue in the newly released Typescript 2.7 release. I did not have this issue with Typescript 2.6.
TypeScript Version: 2.7.0
Search Terms:
TS2339
'never'
'classlist'
Code
import * as ko from 'knockout';
ko.bindingHandlers.validationProperty = {
init(element: HTMLInputElement, valueAccessor: () => string, allBindingsAccessor: KnockoutAllBindingsAccessor, viewModel: any, bindingContext: KnockoutBindingContext) {
const validatable = {modelState:()=>{return {} as any;}},
property = valueAccessor();
if (!validatable) {
return;
}
function mark(validationState: string|undefined) {
if ('setCustomValidity' in element) {
element.setCustomValidity(validationState ? validationState : '');
if (!validationState) {
element.removeAttribute('required');
} else {
element.setAttribute('required', 'required');
}
} else {
if (validationState) {
element.classList.add('is-invalid');
} else {
element.classList.remove('is-invalid');
}
}
}
ko.computed(() => {
const modelState = validatable.modelState();
if (!modelState) {
return;
}
const propertyState = modelState[property];
if (!propertyState) {
mark(undefined);
} else {
mark(propertyState.join('\r\n'));
}
}).extend({ disposeWhenNodeIsRemoved: element });
}
};
tsc --noEmit --pretty ./js/AppFramework/Forms/ValidationMessage.ts
js/AppFramework/Forms/ValidationMessage.ts:23:29 - error TS2339: Property 'classList' does not exist on type 'never'.
23 element.classList.add('is-invalid');
~~~~~~~~~
js/AppFramework/Forms/ValidationMessage.ts:25:29 - error TS2339: Property 'classList' does not exist on type 'never'.
25 element.classList.remove('is-invalid');
~~~~~~~~~
Expected behavior:
No compilation error. In the if branch as shown in the compilation error, element
was never re-assigned to 'never'. Also, the branch is not impossible to execute which would otherwise trigger this error.
Actual behavior:
Compilation error occurs:
TS2339: Property 'classList' does not exist on type 'never'
Playground Link:
Text too large to share, so created Gist instead with full repro:
https://gist.github.com/Sebazzz/aaa19b9793ba80e34acd5e900a3e0c81
If you put both the .d.ts file and .ts file in the playground, the offending lines are highlighted however. For fully repro I did include the tsconfig but it does not appear to be relevant.