Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'T' does not satisfy the constraint 'string | number | symbol'. Type 'T' is not assignable to type 'symbol'

Tags:

typescript

I have the following code, and I receive the following error

Type 'T' does not satisfy the constraint 'string | number | symbol'. Type 'T' is not assignable to type 'symbol'.

I would like to know how to fix the generic so I can pass a union Record

   export type Project<T> = Readonly<{
      dirs: Record<T, string>
      setup: () => Promise<string>
    }>
    
    type Dirs = 'a' | 'b' | 'c'
    
    type Start = Project<Dirs>
like image 645
Radex Avatar asked Oct 26 '25 16:10

Radex


1 Answers

If you need this constraint, then you simply have to add it. Maybe you could even make it more restrictive (removing symbol, for example):

export type Project<T extends string | number | symbol> = Readonly<{
      dirs: Record<T, string>
      setup: () => Promise<string>
    }>
    
    type Dirs = 'a' | 'b' | 'c'
    
    type Start = Project<Dirs>

Please also note that I don't understand what you want to do with Record<T, string>. Record is used to only keep some keys in a given object type. Do you actually simply mean dirs: T? If so, you can do it like this:

export type Project<T extends string> = Readonly<{
      dirs: T
      setup: () => Promise<string>
    }>
    
    type Dirs = 'a' | 'b' | 'c'
    
    type Start = Project<Dirs>
like image 145
Stéphane Veyret Avatar answered Oct 29 '25 08:10

Stéphane Veyret