I want a type for object that has one and only one property with any key and any type T.
type ObjWithOneProperty<T> // = ?
// OK
const obj1: ObjWithOneProperty<boolean> = {
property1: true
}
// OK
const obj2: ObjWithOneProperty<boolean> = {
property2: true
}
// OK (I know tsc wont check this, but it's what I want to express)
const f = (key: string): ObjWithOneProperty<boolean> => {
let obj = {}
obj[key] = true
return obj
}
// Type error
const obj2: ObjWithOneProperty<boolean> = {}
// Type error
const obj3: ObjWithOneProperty<boolean> = {
property1: true,
property2: true
}
Is this possible in Typescript?