microsoft / microsoft/TypeScript

Type the tagName string property of Element derivations to aid overloading and for discriminated unions

Open
#26,860 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

tagName, nodeName

Suggestion

Typing the tagName property in lib.dom.d.ts for Element and derivations ( or only concrete derivations and a discriminated type alias of these )
e.g
interface SVGGElement extends SVGGraphicsElement {
//..existing definition
tagName:"g"
}

Use Cases

Simpler to write overloads, overloaded by single argument type ( order does not matter )
Discriminated Unions

Examples

interface SVGCircleElement{
tagName:"circle"
}
interface SVGGElement{
tagName:"g"
}
interface SVGRectElement{
tagName:"rect"
}

Without above order matters with overload
adopt(node: SVGGElement): G;//issue with this before circle overload
adopt(node: SVGCircleElement): Circle;

With above and
type SVGTestUnionType=SVGCircleElement|SVGRectElement;

function(circleOrG:SVGTestUnionType){
switch(circleOrG.tagName){
case "circle":
//circle members available
break;
case "rect":
//rect members available
break;
}
}

Checklist

My suggestion meets these guidelines: YES

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib.dom.d.ts and review the Element hierarchy and concrete SVG element declarations mentioned in the issue. Determine the intended scope for literal tagName types and how the declarations should support overloads and discriminated unions. Done means the relevant declarations provide the requested narrowing without changing runtime JavaScript behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.