Skip to content

please reconsider to type CSS width and height attribute more strictly #1563

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

Open
scarf005 opened this issue May 20, 2023 · 3 comments
Open

Comments

@scarf005
Copy link

scarf005 commented May 20, 2023

This is a continuation of #1179.

Today, I encountered a bug because the component was passed a string ("300") instead of a number (300) or a properly formatted string ("300px"). This error went unnoticed because width and height attributes are currently typed as number | string | undefined.

While I understand that implementing stricter typing could break older code (refer to #1179 (comment)), I believe that the value of TypeScript lies in providing strict, safe typing to prevent potential type errors.

Therefore, i propose we make the type for width and height more strict like as said in #1179 using template literal types.

type Length = number | `${number}${'px'|'cm'|'mm'|'em'}`

I am open to creating a PR for this issue.

@turansky
Copy link

It's common question for CSS properties.
Length has multiple variants, which can't be described by simple template :(

@Bashamega
Copy link
Contributor

Bashamega commented May 18, 2025

Hi @turansky and @saschanaz,

I’ve put together a working solution for this issue and would love your feedback. You can review the implementation here:

https://github.com/Bashamega/css‑measurements

If it looks useful, let’s discuss integrating it into the main project.

Thanks!

@saschanaz
Copy link
Contributor

I think the proper solution here is CSS Typed OM, which we already have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants