Consider the following Typescript code snippet:
class Excel {
Password: string;
Sheet: number;
}
class Csv {
Separator: string;
Encoding: string;
}
type FileType = Excel | Csv
let input = '{"Separator": ",", "Encoding": "UTF-8"}';
let output = Object.setPrototypeOf(JSON.parse(input), FileType.prototype) // error!
When deserializing from JSON in TypeScript/Javascript, Object.setPrototypeOf()
can be used. However, with a Discriminated Union like the one above, an error is encountered:
error TS2693: 'FileType' only refers to a type, but is being used as a value here.
Question:
- Is there a method to deserialize a Discriminated Union in TypeScript?
- If not, what other elegant approach could be taken to achieve the scenario described above (where two classes,
Excel
andCsv
, need to be instantiated correctly from a JSON string regardless of the technique used)?
Environment
- Typescript v2.9.2
- Visual Studio Code v1.25.1
Proposed Solution
let json = JSON.parse(input);
let output: FileType | null = null;
if (json["Separator"]) {
console.log("It's csv");
output = Object.setPrototypeOf(json, Csv.prototype)
} else if (json["Password"]) {
console.log("It's excel");
output = Object.setPrototypeOf(json, Excel.prototype)
} else {
console.log("Error");
}
This current method involves repetitive checks using if else
statements for each class, which can become cumbersome and requires unique field identification for each class.