1.0.9 • Published 4 years ago

typescript-cast v1.0.9

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
4 years ago

typescript-cast

Tired of interface casts not giving you the expected results? Here's a simple package that should resolve that.

For example:

interface MyInterface {
    myProperty: number;
}

const x = JSON.parse("{ myProperty: \"100\" }");

Beginners to Typescript might expect that x.myProperty would be a number with 100 as its value, but this is incorrect; it's actually a string with "100" as its value. One hack around this is to use reinterpret_cast, which this package provides:

import reinterpret_cast from "typescript-cast";

interface MyInterface {
    myProperty: number;
}

const x = reinterpret_cast<MyInterface>(JSON.parse("{ myProperty: \"100\" }"), { myProperty: "number" });

The second parameter to reinterpret_cast is a list of properties and what type they should be coerced into. Currently, only number and Date are supported.

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago