'typescript infer and narrow down element of Array

type elementOf<T extends unknown[]>=T extends (infer A)[]? A : never

const a = ['abc']

type resultA = elementOf<typeof a> //string

I need to infer the literal type of array element, but it doesn't work

const assertion or type notation did the trick

const b:'abc'[] = ['abc' as const]

type resultB = elementOf<typeof b> // 'abc'

however is it possible to do it without const assertion or type annotation?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source