Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TypeScript Template Types Date Validator
// Check it out here:
https://www.typescriptlang.org/play?ts=4.1.0-dev.20201001#code/C4TwDgpgBAcgrgWwEYQE4GcoF4oHIAMuUAPngIxGm4BMleAzHbgCxMCsTAbEwOxMAcTAJy4A3AChQkWAHsAdgC00M7FACiADwDGAGzgATCAB54yNOgA0eQgD4Jk8NACaEAIapVAAwAkAb1MoGAC+fgHmIf6IgegRYcGeElLQALLywAAWmDie+KHySqgyQZ4k5ISluGQUFWS0iY5QACKuIFlQPr4ETNVUtLH5ysUV9OVU9BT2SVAAEjJwGF65kWbxpZ5koVHhJb2jeNQ9+7QV1IwnrPXSCvIQs-NtMAOFa0uPioM7Zd2HlbQO0skAJZyODACBtDpdGpMY5jJisCocWJbeKXaAAZQgWnk+jaQJBYPQaKgQJ0OkB6CxOIhmxWMVp0WRdOKkwa1zkEAACqgIAAzQEaVS4ADUTAAtGJxP9oM0wZz3JS1KhCqgjMlwehXABzaAQDRguS4qDoYCoYFamyqXxQNAqgBcJI12ugQVZ0llEAAKoCEMZxFAA1BPTb9RBDZgTWa5FrxJacP7A8G9QajR1gby0E1XGDvb6ggo-OnM+yIEMAPxQBOBwPyjAQD25iAlowNn0QKwly12qtBkMpzBpuQZjwe8s96u1ymt32PZseruV6seycQJUqoyeacQB0ASTkADdXOT9Fmc22HX5PcU7FKpiut82e1v0aa+2GjZHzRYeyWXx5k++Eaml+PYrvoW6qPe2Zem2s43C20GNn+NjfhOCoQPoJaQehza-qaNixthdbgYhbZvuGp4wb6f5QGW441uhmE3ORRp4R4dHVpxgYdGBW75n4YElkM3ZcQGgnMSJDHEVubrQFBZ4zjIj5LqRilsSxQFRjGcaLoGW5wRyNEARRg7DpRQSeoWQ6Zo25a6Vx8lUcYHpWI2XZPqpEAGRARmhiZVlmaOJQcaJjkIWCC4eXK6FrjIqqeI5jbebuB5HoCJ4ehevj6UpNx-teslQIlbZGFFPmvsZH7AdGqGJm2vn9s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Numbers}${Numbers}${Numbers}`;
type Months = `0${NonZero}` | '10' | '11' | '12';
type Days = `${'0' | '1' | '2'}${NonZero}` | '30' | '31';
type Hours = `0${Numbers}` | `1${Numbers}` | '20' | '21' | '22' | '23' | '24';
type ZoneHours = NonZero | `0${NonZero}` | '10' | '11' | '12'
type Minutes = `${'0' | '1' | '2' | '3' | '4' | '5'}${Numbers}`;
type Seconds = Minutes;
type Milliseconds = `${Numbers}${Numbers}${Numbers}`;
type ZonePrefix = '+' | '-';
type DateParseError<Message extends string> = { error: Message };
type DateTime<
T extends string
> =
T extends `${infer DateTime}Z${infer Zone}` ?
ParseDateTimeZone<DateTime, Zone> :
T extends `${infer Date}` ?
ParseDateTimeNoZone<Date> :
DateParseError<`DateTime: Invalid DateTime: ${T}`>;
type ParseDateTimeZone<
DateTimeStr extends string,
ZoneStr extends string,
ParsedDateTime = ParseDateTimeNoZone<DateTimeStr>,
ParsedZone = ParseZone<ZoneStr>
> = ParsedDateTime extends DateTimeStr ?
ParsedZone extends ZoneStr ?
`${ParsedDateTime}Z${ParsedZone}` :
ParsedZone :
ParsedDateTime;
type ParseDateTimeNoZone<
DateTimeNoZoneStr extends string
> =
DateTimeNoZoneStr extends `${infer Date}T${infer Time}` ?
ParseDateTime<Date, Time> :
DateTimeNoZoneStr extends `${infer Date}` ?
ParseDate<Date> :
DateParseError<`ParseDateTimeNoZone: Invalid Date: ${DateTimeNoZoneStr}`>;
type ParseDateTime<
DateStr extends string,
TimeStr extends string,
ParsedDate = ParseDate<DateStr>,
ParsedTime = ParseTime<TimeStr>
> = ParsedDate extends DateStr ?
ParsedTime extends TimeStr ?
`${ParsedDate}T${ParsedTime}` :
ParsedTime :
ParsedDate;
type ParseDate<
DateStr extends string
> =
DateStr extends `${infer YearStr}-${infer MonthStr}-${infer DaysStr}` ?
ParseYearMonthDay<YearStr, MonthStr, DaysStr> :
DateStr extends `${infer YearStr}-${infer MonthStr}` ?
ParseYearMonth<YearStr, MonthStr> :
DateStr extends `${infer YearStr}` ?
ParseYear<YearStr> :
DateParseError<`ParseDate: Invalid Date: ${DateStr}`>;
type ParseYearMonthDay<
YearStr extends string,
MonthStr extends string,
DayStr extends string,
ParsedYear = ParseYear<YearStr>,
ParsedMonth = ParseMonth<MonthStr>,
ParsedDay = ParseDay<DayStr>,
> = ParsedYear extends YearStr ?
ParsedMonth extends MonthStr ?
ParsedDay extends DayStr ?
`${YearStr}-${MonthStr}-${DayStr}` :
ParsedDay :
ParsedMonth :
ParsedYear;
type ParseYearMonth<
YearStr extends string,
MonthStr extends string,
ParsedYear = ParseYear<YearStr>,
ParsedMonth = ParseMonth<MonthStr>,
> = ParsedYear extends YearStr ?
ParsedMonth extends MonthStr ?
`${YearStr}-${MonthStr}` :
ParsedMonth :
ParsedYear;
type ParseYear<
YearStr extends string,
> =
YearStr extends Year ?
YearStr :
DateParseError<`ParseYear: Invalid Year: ${YearStr}`>;
type ParseMonth<
MonthStr extends string,
> =
MonthStr extends Months ?
MonthStr :
DateParseError<`ParseMonth: Invalid Month: ${MonthStr}`>;
type ParseDay<
DayStr extends string,
> =
DayStr extends Days ?
DayStr :
DateParseError<`ParseDay: Invalid Date: ${DayStr}`>;
type ParseZone<
ZoneStr extends string
> =
ZoneStr extends ZoneHours ?
ZoneStr :
ZoneStr extends `${ZonePrefix}${infer Offset}` ?
ParseZoneOffset<Offset> extends Offset ?
ZoneStr :
ParseZoneOffset<Offset> :
ZoneStr extends `${infer Offset}` ?
ParseZoneOffset<Offset> extends Offset ?
ZoneStr :
ParseZoneOffset<Offset> :
DateParseError<`ParseZone: Invalid Zone: ${ZoneStr}`>
type ParseZoneOffset<
OffsetStr extends string,
> =
OffsetStr extends `${infer Hours}:${infer Minutes}` ?
ParseHoursMinutes<Hours, Minutes> :
OffsetStr extends `${infer Hours}` ?
ParseHours<Hours> :
DateParseError<`ParseZoneOffset: Invalid Offset: ${OffsetStr}`>;
type ParseTime<
TimeStr extends string
> =
TimeStr extends `${infer HoursStr}:${infer MinutesStr}:${infer SecondsStr}.${infer MillisStr}` ?
ParseHoursMinutesSecondsMillis<HoursStr, MinutesStr, SecondsStr, MillisStr> :
TimeStr extends `${infer HoursStr}:${infer MinutesStr}:${infer SecondsStr}` ?
ParseHoursMinutesSeconds<HoursStr, MinutesStr, SecondsStr> :
TimeStr extends `${infer HoursStr}:${infer MinutesStr}` ?
ParseHoursMinutes<HoursStr, MinutesStr> :
DateParseError<`ParseTime: Invalid Time: ${TimeStr}`>;;
type ParseHoursMinutesSecondsMillis<
HoursStr extends string,
MinutesStr extends string,
SecondsStr extends string,
MillisStr extends string,
ParsedHours = ParseHours<HoursStr>,
ParsedMinutes = ParseMinutes<MinutesStr>,
ParsedSeconds = ParseSeconds<SecondsStr>,
ParsedMillis = ParseMillis<MillisStr>
> =
ParsedHours extends HoursStr ?
ParsedMinutes extends MinutesStr ?
ParsedSeconds extends SecondsStr ?
ParsedMillis extends MillisStr ?
`${HoursStr}:${MinutesStr}:${SecondsStr}.${MillisStr}` :
ParsedMillis :
ParsedSeconds :
ParsedMinutes :
ParsedHours;
type ParseHoursMinutesSeconds<
HoursStr extends string,
MinutesStr extends string,
SecondsStr extends string,
ParsedHours = ParseHours<HoursStr>,
ParsedMinutes = ParseMinutes<MinutesStr>,
ParsedSeconds = ParseSeconds<SecondsStr>
> =
ParsedHours extends HoursStr ?
ParsedMinutes extends MinutesStr ?
ParsedSeconds extends SecondsStr ?
`${HoursStr}:${MinutesStr}:${SecondsStr}` :
ParsedSeconds :
ParsedMinutes :
ParsedHours;
type ParseHoursMinutes<
HoursStr extends string,
MinutesStr extends string,
ParsedHours = ParseHours<HoursStr>,
ParsedMinutes = ParseMinutes<MinutesStr>
> =
ParsedHours extends HoursStr ?
ParsedMinutes extends MinutesStr ?
`${HoursStr}:${MinutesStr}` :
ParsedMinutes :
ParsedHours;
type ParseHours<
HourStr extends string,
> =
HourStr extends Hours ?
HourStr :
DateParseError<`ParseHours: Invalid Hour: ${HourStr}`>;
type ParseMinutes<
MinutesStr extends string,
> =
MinutesStr extends Minutes ?
MinutesStr :
DateParseError<`ParseMinutes: Invalid Minutes: ${MinutesStr}`>;
type ParseSeconds<
SecondsStr extends string,
> =
SecondsStr extends Seconds ?
SecondsStr :
DateParseError<`ParseSeconds: Invalid Seconds: ${SecondsStr}`>;
type ParseMillis<
MillisStr extends string,
> =
MillisStr extends Milliseconds ?
MillisStr :
DateParseError<`ParseMillis: Invalid Milliseconds: ${MillisStr}`>;
type MinYear = DateTime<'0000'>;
type MaxYear = DateTime<'9999'>;
type YearError = DateTime<'19999'>;
type YearZone = DateTime<'1999Z1'>;
type YearZoneHour = DateTime<'1999Z01'>;
type YearZoneHoursMinutes = DateTime<'1999Z01:15'>;
type YearZoneNegative = DateTime<'1999Z-01'>;
type YearZoneHoursMinutesNegative = DateTime<'1999Z-01:15'>;
type YearZonePositive = DateTime<'1999Z+24'>;
type YearZoneHoursMinutesPositive = DateTime<'1999Z+01:15'>;
type YearMonth = DateTime<'1999-03'>;
type YearMonthError = DateTime<'1999+03'>;
type YearInvalidMonthErrorSmall = DateTime<'1999-00'>;
type YearInvalidMonthErrorBig = DateTime<'1999-13'>;
type YearMonthZone = DateTime<'1999-03Z1'>;
type YearMonthZoneHour = DateTime<'1999-03Z01'>;
type YearMonthZoneNegative = DateTime<'1999-03Z-01'>;
type YearMonthZonePositive = DateTime<'1999-03Z+24'>;
type YearMonthDayError = DateTime<'1999-03+03'>;
type YearMonthDay = DateTime<'1999-03-03'>;
type YearMonthInvalidDayErrorSmall = DateTime<'1999-03-00'>;
type YearMonthInvalidDayErrorBig = DateTime<'1999-03-32'>;
type YearMonthDayZone = DateTime<'1999-03-03Z1'>;
type YearMonthDayZoneHour = DateTime<'1999-03-03Z01'>;
type YearMonthDayZoneNegative = DateTime<'1999-03-03Z-01'>;
type YearMonthDayZonePositive = DateTime<'1999-03-03Z+24'>;
type YearMonthDayHourError = DateTime<'1999-03-03T01'>;
type YearMonthDayHourMinute = DateTime<'1999-03-03T01:00'>;
type YearMonthDayHourMinuteErrorSmall = DateTime<'1999-03-03T01:1'>;
type YearMonthDayHourMinuteErrorBig = DateTime<'1999-03-03T01:60'>;
type YearMonthDayHourMinuteZone = DateTime<'1999-03-03T01:59Z1'>;
type YearMonthDayHourMinuteZoneHour = DateTime<'1999-03-03T01:59Z03'>;
type YearMonthDayHourMinuteZoneNegative = DateTime<'1999-03-03T01:59Z-02'>;
type YearMonthDayHourMinuteZonePositive = DateTime<'1999-03-03T01:59Z+23'>;
type YearMonthDayHourMinuteSecond = DateTime<'1999-03-03T01:59:59'>;
type YearMonthDayHourMinuteSecondErrorSmall = DateTime<'1999-03-03T01:59:0'>;
type YearMonthDayHourMinuteSecondErrorBig = DateTime<'1999-03-03T01:59:60'>;
type YearMonthDayHourMinuteSecondZone = DateTime<'1999-03-03T01:59:40Z6'>;
type YearMonthDayHourMinuteSecondZoneHour = DateTime<'1999-03-03T01:59:40Z07'>;
type YearMonthDayHourMinuteSecondZoneHourNegative = DateTime<'1999-03-03T01:59:40Z-04'>;
type YearMonthDayHourMinuteSecondZoneHourPositive = DateTime<'1999-03-03T01:59:40Z+05'>;
type YearMonthDayHourMinuteSecondMilli = DateTime<'1999-03-03T01:59:59.000'>;
type YearMonthDayHourMinuteSecondMilliErrorSmall = DateTime<'1999-03-03T01:59:59.0'>;
type YearMonthDayHourMinuteSecondMilliErrorBig = DateTime<'1999-03-03T01:59:59.1000'>;
type YearMonthDayHourMinuteSecondMilliZone = DateTime<'1999-03-03T01:59:59.000Z7'>;
type YearMonthDayHourMinuteSecondMilliZoneHour = DateTime<'1999-03-03T01:59:59.000Z08'>;
type YearMonthDayHourMinuteSecondMilliZoneNegative = DateTime<'1999-03-03T01:59:59.000Z-09'>;
type YearMonthDayHourMinuteSecondMilliZonePositive = DateTime<'1999-03-03T01:59:59.000Z+10'>;
@Realetive

This comment has been minimized.

Copy link

@Realetive Realetive commented Oct 5, 2020

Wow! Cool! May be publish it as npm-package to @types/ParseDate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment