Type CSS and HTML lengths with templates
See original GitHub issueI 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:
- Created 2 years ago
- Comments:9 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, I think this makes sense (thanks @HolgerJeromin) maybe I can make this idea live on in a TypeScript Halloween Code challenge this week
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.