Why does TypeScript infer the ‘never’ type when reducing an Array with concat?

I believe this is because the type for [] is inferred to be never[], which is the type for an array that MUST be empty. You can use a type cast to address this: [‘a’, ‘b’, ‘c’].reduce((accumulator, value) => accumulator.concat(value), [] as string[]); Normally this wouldn’t be much of a problem since TypeScript does a … Read more

How to assert a type of an HTMLElement in TypeScript?

TypeScript uses ‘<>’ to surround casts, so the above becomes: var script = <HTMLScriptElement>document.getElementsByName(“script”)[0]; However, unfortunately you cannot do: var script = (<HTMLScriptElement[]>document.getElementsByName(id))[0]; You get the error Cannot convert ‘NodeList’ to ‘HTMLScriptElement[]’ But you can do : (<HTMLScriptElement[]><any>document.getElementsByName(id))[0];

Accept any object as argument in function

I don’t completely agree with the premise of the default configuration of typescript-eslint’s ban-types rule which says that Avoid the object type, as it is currently hard to use due to not being able to assert that keys exist. See microsoft/TypeScript#21732. As the person who filed the linked issue, I do understand that it is … Read more