const string or const number as a type between string (or number) and literal types #51745
Closed
5 tasks done
Labels
Duplicate
An existing issue was already created
Suggestion
π Search Terms
literal string, const literals, infer literals
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
The same applies to numbers.
π Motivating Example
I want to define an API for users to specify a fixed path in an object, consider
Currently, typescript would infer this
set
call'sP
asstring[]
, hence value will be inferred asnever
.This however, works when
P extends string
, so one way of doing this would beThis feels both cumbersome and not scalable.
With the proposed change, it'll look like
On the other hand, sometimes people do want their generics to be inferred as
string
, not the literal passed in.Allowing a
const
bound would allow developers to express precisely what they want.π» Use Cases
The text was updated successfully, but these errors were encountered: