For example, I have
type Line = {
start: Point;
end: Point;
color: string; //'cyan'/'aquablue'/...
}
But now I want to create new line type on the base of Line, so that it stores color as number:
type HexColorLine = Point & {
color: number;
}
Now I expect the HexColorPoint type be equal to
{
start: Point;
end: Point;
color: number;
}
But it equals to
{
start: Point;
end: Point;
color: string | number;
}
Is there a way to override but not extend the prop type with some short syntax? Do i really have to define entirely new type for this?
To override a class method in TypeScript, extend from the parent class and define a method with the same name. Note that the types of the parameters and the return type of the method have to be compatible with the parent's implementation. Copied! class Parent { doMath(a: number, b: number): number { console.
The process of changing types is what is called “type casting”. Typecasting in strongly typed languages like C# allow us to convert types. string numStr = "123"; int numNum; bool isParsable = Int32. TryParse(numStr, out numNum);
The typescript type supports only the data types and not the use of an object. The typescript interface supports the use of the object.
Create a helper type:
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
Usage:
type HexColorLine = Overwrite<Line, { color: number }>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With