Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

interface object couldn't extends Record<string, unknown>

Tags:

typescript

why interface couldn't extend Record

interface Data {
  a: string
}

Data extends Record<string, unknown> ? 'yes' : 'no' // 'no'

however, if I change the Data to type it works fine

type Data {
  a: string
}
 
Data extends Record<string, unknown> ? 'yes' : 'no' // 'yes'
like image 298
Bill Avatar asked Oct 31 '20 11:10

Bill


Video Answer


1 Answers

type T = Record<string, unknown> evaluates to { [x: string]: unknown; }

Object types have an implicit index signature, but interfaces don't (for safety reasons), so they don't extend an index type.

like image 88
lissitz Avatar answered Oct 23 '22 05:10

lissitz