Skip to content

ShadowRoot interface and attachShadow function are missing from type definitions #10401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Aug 18, 2016 · 13 comments
Closed
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@ghost
Copy link

ghost commented Aug 18, 2016

TypeScript Version: 1.8.0 / nightly (2.0.0-dev.201xxxxx)

Code

// A *self-contained* demonstration of the problem follows...
class CustomElement extends HTMLElement {
    private shadowDom : ShadowRoot;

    createdCallback() : void {
        this.shadowDom = this.attachShadow({mode: "closed"});
    }
}

Expected behavior:
ShadowRoot is recognized as a valid type, and the HTMLElement's attachShadow function is recognized as per the DOM standard.

Actual behavior:
Neither ShadorRoot nor the attachShadow functions are recognized.

@kitsonk
Copy link
Contributor

kitsonk commented Aug 18, 2016

The Shadow DOM is still not implemented in many browsers. Interfaces are open ended, so these can be declared yourself.

@mhegazy mhegazy added Suggestion An idea for TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Revisit An issue worth coming back to labels Aug 31, 2016
@bradleyayers
Copy link

bradleyayers commented Oct 25, 2016

@Fairmill here's what I ended up using in my project: (based on https://www.w3.org/TR/shadow-dom/)

// shadow-dom.d.ts
declare global {
    type ShadowRootMode = "open" | "closed";

    interface ShadowRootInit {
        mode: ShadowRootMode;
        delegatesFocus?: boolean;
    }

    interface CaretPosition {
        offsetNode: Node;
        offset: number;
        getClientRect(): ClientRect;
    }

    interface DocumentOrShadowRoot {
        getSelection(): Selection | null;
        elementFromPoint(x: number, y: number): Element | null;
        elementsFromPoint(x: number, y: number): NodeListOf<Element>;
        caretPositionFromPoint(x: number, y: number): CaretPosition | null;
        activeElement: Element | null;
        styleSheets: StyleSheetList;
    }

    interface ShadowRoot extends DocumentFragment, DocumentOrShadowRoot {
        host: HTMLElement;
        mode: ShadowRootMode;
    }

    interface Document extends DocumentOrShadowRoot {}

    interface AssignedNodesOptions {
        flatten: boolean = false;
    }

    interface HTMLSlotElement extends HTMLElement {
        name: string;
        assignedNodes(options?: AssignedNodesOptions): NodeList;
    }

    interface Element {
        attachShadow(shadowRootInitDict: ShadowRootInit): ShadowRoot;
        assignedSlot: HTMLSlotElement | null;
        slot: string;
        shadowRoot: ShadowRoot | null;
    }
}

export { }

@marcoms
Copy link

marcoms commented Jul 19, 2017

Could this be revisited? V1 seems a lot better on caniuse - http://caniuse.com/#feat=shadowdomv1

@klebba
Copy link

klebba commented Jun 14, 2018

@kitsonk

Please consider reopening this issue -- ShadowDOM V1 support is now imminent in FireFox and has already landed in Chrome and Safari:

@emilio-martinez
Copy link

I agree. This issue is at a point where it should be revisited soon.

@kitsonk
Copy link
Contributor

kitsonk commented Jun 24, 2018

@klebba I am not part of the TypeScript team.

@kitsonk
Copy link
Contributor

kitsonk commented Jun 24, 2018

And the issue is still open.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 25, 2018

This should be fixed in typescript@next already.

@mhegazy mhegazy closed this as completed Jun 25, 2018
@mhegazy mhegazy added Fixed A PR has been merged for this issue and removed Revisit An issue worth coming back to labels Jun 25, 2018
@mhegazy mhegazy added this to the TypeScript 3.0 milestone Jun 25, 2018
@SetTrend
Copy link

SetTrend commented Mar 19, 2019

@mhegazy : It looks like ShadowRoot still isn't implemented in the library declarations, or getRootNode() seems to return the wrong type (Node instead of ShadowRoot).

When I write ...

let h = this.getRootNode().host;

... I get a TS2339 compiler error: "Property 'host' not available for type 'Node'."

@17biubiu
Copy link

me too, the version of ts is typescript@3.4.5. Property 'createShadowRoot' does not exist on type 'HTMLElement'。how to solve it?

@SetTrend
Copy link

SetTrend commented May 23, 2019

In the meantime I got a great explanation why this doesn't work the way we all expect:

whatwg/dom#739

MDN already updated their docs to clarify the situation.

@bennypowers
Copy link

Until TypeScript corrects their lib, we'll have to

declare global {
  interface Node {
    getRootNode(options?: GetRootNodeOptions): Node|ShadowRoot;
  }
}

@SetTrend
Copy link

Actually, Node is a base class of ShadowRoot. So, the current interface is correct.

See: https://www.w3.org/TR/dom41/#shadowroot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

9 participants