Typescript Caveat 1: Mixing String Literal Key And String Key In Object Literal Type

type A = {[x:string]:1, a1:1, a2:2} // error: Property 'a2' of type '2' is not assignable to 'string' index type '1'.type a = A['something'] // 1
// ^?
type a1 = A['a1'] // 1
// ^?
type a2 = A['a2'] // hmm, we get 2, but it is useless
// ^?
const A:A={something:1, a1:1, a2:2} // error: Type '2' is not assignable to type '1'.
type B = {[x:string]:1} & { b1:1, b2:2} // no errortype b = B['something']
// ^?
type b1 = B['b1']
// ^?
type b2 = B['b2']
// ^?
const B:B={something:1, b1:1, b2:2} // error: Type '2' is not assignable to type '1'.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store