#Type 'number' is not assignable to type 'Space'.

1 messages · Page 1 of 1 (latest)

teal pathBOT
#
withlume#0

Preview:```ts
type NumberToString<T extends number> = ${T}
type Space = 0 | 4 | 8 | 12 | 16 | 20 | 24 | 28

function toNumber(
value: NumberToString<Space> | Space
): Space {
if (typeof value === "string") {
return Number(value) // i could do type casting, but is it "best practice
...```

light hamlet
#

Type 'number' is not assignable to type 'Space'

#

Type 'number' is not assignable to type 'Space'.

coarse pumice
#

you could do extra runtime validation. here's an example with a isOneOf helper function that i sometimes use:

teal pathBOT
#
mkantor#0

Preview:ts ... function toNumber( value: NumberToString<Space> | Space ): Space { if (typeof value === "string") { const result = Number(value) if (isSpace(result)) { return result } else { throw new Error("invalid value") } } return value }