Closed Mesoptier closed 2 years ago
const valid: Duration = 'P1Y2M4DT20H44M12,67S';
this is a valid string, but shows as invalid in your typing.
It's a very cool idea about what can be done with template string types, but... seems like a lot of work to get that beast of a type to work as expected, PR's are very welcome though 😉
Not to mention, it doesn't allow P1Y
to be a valid type either. Though that would be solved with another union without the T section.
TypeScript has support for constructing string types using template strings (Template Literal Types). Those types could be used to construct a string type that allows only valid ISO8601-duration strings instead of any arbitrary string.
For example, something like this may work:
Code example on TS Playground
That being said, I'm not entirely sure if this would actually be useful, since in most use cases the duration strings come from external APIs and so are not typechecked at compilation time. What do you think?