natsuki_2002
natsuki_2002

Reputation: 25359

Typescript: Custom type that parallels base type ignored

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;
};

Reality: 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

Answers (1)

Steve Rock
Steve Rock

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

Related Questions