My current objective with the code below:
interface SOME_OBJECT_DATE { otherProp: boolean, myDate: Date }
interface SOME_OBJECT_STRING { otherProp: boolean, myDate: string }
function convertDateToString(obj: SOME_OBJECT_DATE): SOME_OBJECT_STRING {
const newObj = {...obj} as SOME_OBJECT_STRING;
newObj.myDate = obj.myDate.toJSON();
return newObj;
}
The primary aim of this function is to change the type of the myDate
property from Date
to string
. It also entails copying the remaining object properties, namely the otherProp
property.
How can I modify the value of myDate
, duplicate the obj
parameter, and ensure the correct typing?
https://i.sstatic.net/V1VVy.png
This is the specific error message that I'm encountering:
https://i.sstatic.net/oDW5K.png
Please note: In my actual scenario, there are additional overloads (various types extending the "base" interface { myDate: Date }
). Therefore, I have created a list of these overloads that will be utilized. However, I still need to ensure proper typing of the implementation, similar to the example provided above.