Problem233
Problem233

Reputation: 31

Type for object that has one and only one property

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?

Upvotes: 3

Views: 1071

Answers (1)

Shanon Jackson
Shanon Jackson

Reputation: 6581

interface IPropertyOne<T> {
    property1: T;
}

// OK
const obj1: IPropertyOne<boolean> = {
  property1: true
}

// Type error
const obj2: IPropertyOne = {}

// Type error
const obj3: IPropertyOne<boolean> = {
  property1: true,
  property2: true
}

this is the static key version do you want dynamic key?

EDIT: Dynamic key version

export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void)
    ? I
    : never;
type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
type ISingleKey<K extends string, T> = IsUnion<K> extends true ? "Can only contain a single key" : Record<K, T>;

// OK
const obj1: ISingleKey<"property1", boolean> = {
  property1: true
}

// Type error
const obj2: ISingleKey<"property1", boolean> = {}

// Type error
const obj3: ISingleKey<"property1", boolean> = {
  property1: true,
  property2: true
}

// Type error 2 keys.
const obj4: ISingleKey<"property1" | "property2", boolean> = {
  property1: true,
  property2: true
}

Upvotes: 2

Related Questions