I try to pass a parentNode
of a node to my getStyleComputedProperty
function.
But got an error:
Argument of type '(Node & ParentNode) | null' is not assignable to parameter of type 'Element'. Type 'null' is not assignable to type 'Element'.
Code here:
type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends string ? K : never }[keyof T];
type Property = NonFunctionPropertyNames<CSSStyleDeclaration>;
export const getStyleComputedProperty = (ele: Element, property: Property): string => {
const css = window.getComputedStyle(ele, null);
return css[property];
};
const div = document.createElement('div')
getStyleComputedProperty(div.parentNode, 'position') // tsc throw error
I know I can use type cast like this div.parentNode as Element
to pass the TSC type check. But I wonder how can I make the type correct without the type cast.
Here is TypeScript Playground