class Book {
title: string;
datePublished: Date;
static unserialize(str) {
let ret = JSON.parse(str, (key, value) => {
switch (key) {
case 'datePublished': return new Date(value);
default:return value;
}
}) as Book;
return ret;
}
}
When unseralizing an object you can use the revive function in JSON.parse like in the sample. But you are accessing the properties of the object by name in constant string thus losing the "control" of typescript (for example refactoring changing the name of a prop would not be reflected in the switch cases).
Is there any better way to use typescript's possibilities?