Reputation: 25359
Expectation:
If I create a custom type that parallels an existing type I expect to see variables assigned that type maintain that type and not fallback to to the paralleled base type. The function f
below should return a Dog
type instead a string
type.
type Dog = string;
const f = (dog: Dog): Dog => {
return dog;
};
Is there a reason why Typescript does this? Is this a bug? I realize both approaches are by definition equivalent but I would like to use my custom types for readability. Any thoughts?
Upvotes: 0
Views: 63
Reputation: 372
I think this will help you learn more about aliases.
Aliasing doesn’t actually create a new type - it creates a new name to refer to that type.
Upvotes: 1