TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
const objArr = [{ a: "e" as const, b: 2 }, { a: "foo" as const, b: 100 }, { a: "bar" as const, b: 100 }]
type GetA = (typeof objArr)[number]["a"] // "e" | "foo" | "bar"
const objArr = [{ a: "e", b: 2 }, { a: "foo", b: 100 }, { a: "bar", b: 100 }] as const
I’m not your mom, right?
I have an object that accepts a string
parameter in it's constructor, and it's toString()
formats that string in a specific way.
for instance:
export class Currency
{
constructor(private amt: string) {}
...
toString() { /* return formatted string here, comma separated etc */ }
}
amd if I use an object of this class directly, it returns the formatted string.
But when I try to parse from json
, the Currency
object is treated as string, and doesn't format with it's toString()
method.
Currency
object?
Currency
class is part of a model, and it's being given a string value when the json is JSON.parse
d. Instead of doing this manually, I was wondering if typescript offers some functionality to initialize these objects, maybe in a way where the string is not assigned to the object, but instead passed in as a constructor parameter?