question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type CSS and HTML lengths with templates

See original GitHub issue

I would like to make a suggestion, to make the types more specific for HTML and CSS lengths.

Presently, attributes such as height are typed as being string. They have to allow string rather than number because of the measurement that follows a number, such as 10px or 1.2rem.

I think this can be improved using Typescript templates, by doing something like this instead:

type Length = number | `${number}${'px'|'ch'|'pt'|'cm'|'in'|'em'|''}`;

interface SomeElement {
    height?: Length
}

This would result in:

<SomeElement height={'5px'} /> // allowed
<SomeElement height={'asdf'} /> // compile time error

If this is agreed upon, I will start implementing it.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ortacommented, Oct 27, 2021

Yeah, I think this makes sense (thanks @HolgerJeromin) maybe I can make this idea live on in a TypeScript Halloween Code challenge this week

1reaction
mrbrianevanscommented, Oct 27, 2021

Ah alright, that makes sense. I had not considered such examples.

I am closing this issue, as it seems it would cause many breaking changes to existing projects using Typescript.

Read more comments on GitHub >

github_iconTop Results From Across the Web

grid-template - CSS: Cascading Style Sheets - MDN Web Docs
The grid-template CSS property is a shorthand property for defining grid columns, grid rows, and grid areas.
Read more >
CSS Templates - W3Schools
CSS Layout Templates. We have created some responsive starter templates with CSS. You are free to modify, save, share, and use them in...
Read more >
The Lengths of CSS
There are quite a few properties in CSS that take a length as a value. The box model properties are the obvious ones:...
Read more >
HTML and CSS Tutorial - Nanyang Technological University
There are many good and free CSS templates (or web templates) available online (just google ... An HTML document begins with a Document-Type...
Read more >
CSS Template Layout Module - W3C
CSS values and Units module [CSS3VAL] – <string> and <length> . ... (All mark-up examples in this specification are HTML fragments, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found