It can be argued that the JSDoc comment pertains to the type string in this case, rather than the property itself. This allows for defining a type alias and placing the documentation comment there.
/**
* A date string in the format `yyyy-MM-dd`
*/
type DateString = string;
interface Foo {
archiveDate: DateString;
}
interface Bar {
publishDate: DateString;
}
Furthermore, you have the option to encode the format as a string literal type:
type Digit = '0' | '1' | '2' | '3' // | ... ;
type DateString = `${Digit}${Digit}${Digit}${Digit}-${Digit}${Digit}-${Digit}${Digit}`;
(the task of resolving the error
Expression produces a union type that is too complex to represent.(2590)
when adding the remaining digits is left as an exercise for the reader)
By using this approach, errors will surface when the specified format is not adhered to:
const foo: Foo = {
archiveDate: '2023-1-1'
}
Type '"2023-1-1"' is not assignable to type '"0000-00-00" | "0000-00-01" | "0000-00-02" | "0000-00-03" | "0000-00-10" | "0000-00-11" | "0000-00-12" | "0000-00-13" | "0000-00-20" | "0000-00-21" | "0000-00-22" | "0000-00-23" | ... 65523 more ... | "3333-33-33"'. Did you mean '"2023-01-01"'?(2820)
Playground