You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
typeDuration=| `P${`${number}Y` |''}${`${number}M` |''}${`${number}D` |''}T${`${number}H` |''}${`${number}M` |''}${`${number}S` |''}`
| `P${number}W`;// valid assignmentconstvalid: Duration='P1Y2M4DT20H44M12.67S';// invalid assignment: '"test"' is not assignable to type 'Duration'constinvalid: Duration='test';
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?
The text was updated successfully, but these errors were encountered:
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 😉
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?
The text was updated successfully, but these errors were encountered: