Complete the code to declare a phantom type parameter in a TypeScript interface.
interface Box<[1]> {
value: string;
}The phantom type parameter is a generic type parameter that is not used in the interface's members. Here, T is a placeholder type parameter.
Complete the code to define a phantom type in a TypeScript type alias.
type TaggedString<[1]> = string & { __tag?: [1] };
The phantom type parameter Tag is used to create a unique branded type without affecting the runtime value.
Fix the error in the phantom type usage by completing the generic parameter.
function wrapValue<[1]>(value: string): TaggedString<[1]> { return value as TaggedString<[1]>; }
The generic parameter T must be declared to use the phantom type correctly in the function.
Fill both blanks to create a phantom type that distinguishes between UserId and OrderId.
type [1] = TaggedString<'UserId'>; type [2] = TaggedString<'OrderId'>;
Phantom types UserId and OrderId are created to distinguish between different string types at compile time.
Fill all three blanks to define a function that accepts only UserId and returns a string.
function getUserName(id: [1]): [2] { // pretend to fetch user name return 'User_' + id as [3]; }
The function parameter must be UserId phantom type, and the return type is string. The cast uses string as well.