#Created a string Union type, how to use it without using "as" on my strings?

4 messages · Page 1 of 1 (latest)

slender brook
#

I suspect is this a simple one but I don't know enough of the terminology to google it!

type AllowedLabels = "One"|"Two"|"Three";

const printLabel = (label:AllowedLabels) => { 
  // code
} 

printLabel("Two"); // Argument of type "AllowedLabels" is not assignable to parameter of type "AllowedLabels"

printLabel("Two" as AllowedLabels); // No TS Error?

What are my eyes missing?

autumn jolt
#

The error message doesn't match your code - putting it in the playground doesn't show any error

proven jettyBOT
#
MR#5314

Preview:```ts
type AllowedLabels = "One" | "Two" | "Three"

const printLabel = (label: AllowedLabels) => {
// code
}

printLabel("Two")```

slender brook
#

!resolved