#Map a number wrapped in backticks to a number?

13 messages · Page 1 of 1 (latest)

haughty furnace
#
type SomePath = `company.employees.${number}.name`

type TuplifyPath<S extends string> = 
  S extends `${infer Left}.${infer Right}` 
  ? [Left extends `${infer N}` ? N extends number ? N : Left : Left, ...TuplifyPath<Right>] 
  : [S];

type T = TuplifyPath<SomePath>

const t: T = ['company', 'employees', `${1}`, 'name']

I have been trying to figure out how to just pass 1 instead of ${1} and I am pretty sure infer could be used for that, though I don't really see it working 🥐

spark yarrow
#

infer N is just getting you a string
you need infer N extends number, then you don't need the N extends number

smoky nimbusBOT
#
that_guy977#0

Preview:ts ... ? [ Left extends `${infer N extends number}` ? N : Left, ...TuplifyPath<Right> ] : [S] ...

haughty furnace
#

i see why it's working and mine was not

#

!resolved

spark yarrow
#

though you might want to consider using bigint, since decimals are a thing

smoky nimbusBOT
#
that_guy977#0

Preview:ts ... type T = TuplifyPath<SomePath<number>> // ^? type U = TuplifyPath<SomePath<1>> // ^? type V = TuplifyPath<SomePath<1.2>> // ^? ...

spark yarrow
#

it might make some other stuff messy

#

so.. just something to consider

#

if number will always be an integer and you can control that, it's probably not really an issue

haughty furnace
#

that's real

#

ok as I am using type-fest, I am gonna use Integer type for this