There are a number of ways you can do this.
If you are absolutely sure that at the moment the code runs, the value of contract.LegalReviewDate
is a string with a date formatted appropriately (see this answer by CMS to Why does Date.parse give incorrect results? for more information on cross-browser supported formats), you can use the as
operator to tell TypeScript you are confident of its validity:
contract.LegalReviewDate = new Date(contract.LegalReviewDate as string);
You can also use type guards to make sure that your assumptions are correct:
if (typeof contract.LegalReviewDate === 'string') {
contract.LegalReviewDate = new Date(contract.LegalReviewDate as string);
}
if (typeof contract.LegalReviewDate === 'object' && contract.LegalReviewDate instanceof Date) {
contract.LegalReviewDate = contract.LegalReviewDate as Date;
// OR contract.LegalReviewDate = new Date(contract.LegalReviewDate.valueOf());
}
Or as @jcalz mentions, use two separate properties or objects. For instance,
interface Contract {
LegalReviewDate?: string;
}
interface ContractDTO {
LegalReviewDate?: Date;
}
Then, create a new ContractDTO
that you use to pass data to (and from) SharePoint.