When your code is running, there won't be any noticeable difference between the two types (except for an additional function call to retrieve the default value for the title property). In this scenario, the use of PropType
is unnecessary.
In Vue, the type
key in property definitions acts as a clue for Vue's validation process during component assignment. It requires a constructor that Vue can use for comparison at runtime, issuing a warning if mismatched. This behavior remains consistent whether you are writing code in typescript or plain javascript (though typescript will still detect mismatches like adding a number to a String property during design time).
To ensure correct typing for component properties in typescript, generic javascript constructors alone are insufficient. Therefore, additional information in the form of PropType<>
is included. Vue utilizes this data to determine the exact property type and ensure accurate typing for component instances. However, this manipulation occurs only during design/build time and is disregarded when the code is compiled into javascript.
The utilization of PropType<>
remains advantageous, especially with String, where specific typescript values need to be specified:
type Color = "red" | "blue";
props: {
color: {
type: String as PropType<Color>
}
}
Consequently, Vue verifies at runtime that only strings are assigned to the color property, while typescript ensures during design time that only "blue" or "red" strings can be actually utilized.