20

Is it possible to restrict the count of object properties, say I want to restrict object have only one string propery (with any name), I can do:

{[index: string]: any}

to restrict the type of property, but can one restrict also the count of properties?

maninak
  • 2,633
  • 2
  • 18
  • 33
WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181

5 Answers5

16

There are many answers to this question on Stackoverflow (including this detailed one), but none of them worked for my situation, which is similar to the one posted here.

Problem

I have a function that takes an object. I want it to throw a Compilation Error (Typescript) if the passed object doesn't have exactly one key. e.g.

f({}); // Must error here, as it has less than one key!
f({ x: 5 });
f({ x: 5, y : 6 }); // Must error here, as it has more than one key!

Solution

Using the popular UnionToIntersection and IsUnion, I achieved it via the following utility function.

type SingleKey<T> = IsUnion<keyof T> extends true ? never : {} extends T ? never : T;

Full Code:

// From https://stackoverflow.com/a/50375286
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;

// From: https://stackoverflow.com/a/53955431
type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;

// Here we come!
type SingleKey<T> = IsUnion<keyof T> extends true ? never : {} extends T ? never : T;

// Usage:
function f<T extends Record<string, any>>(obj: SingleKey<T>) {
    console.log({ obj });
}

f({}); // errors here!
f({ x: 5 });
f({ x: 5, y : 6 }); // errors here!

Playground Link

Aidin
  • 25,146
  • 8
  • 76
  • 67
  • 2
    Broken Playground link, you can now find it [here](https://tsplay.dev/mx6Mzw) – Stanislas Dec 16 '21 at 00:09
  • thanks @Stanislas. updated the link – Aidin Dec 16 '21 at 18:00
  • 2
    @Aidin Is there a way to make an array of SingleKey with all different keys? Doing `Array>` makes it so they all need the same key. (e.g. `[{key1: "value1"},{key2:"value1"}]` throws and error because key1!==key2. Which is sadly the exact opposite behavior that I am looking for. – Daniel Mar 04 '22 at 23:36
  • @Daniel that should be possible but it's probably a bit challenging. :) I played a little bit on it but couldn't get anywhere. I highly recommend posting that as a separate question on SO and cross-referencing it here! GL :) – Aidin Mar 07 '22 at 17:18
  • 2
    @Aidin https://stackoverflow.com/questions/71462472/is-there-a-way-to-make-an-array-of-singlekey-with-all-different-keys – Daniel Mar 14 '22 at 02:41
  • This didn't error as expected `const test: SingleKey = { a: 1, b: 2 }`. Though this answer did work: https://stackoverflow.com/questions/46370222 – Mark Swardstrom Jun 10 '22 at 00:50
3

Most likely no. The best solution that comes to my mind is wrapping an Object (or Map) with you custom class with methods set(key: string, val: any) and get(key: string) that can disallow adding new items to the underlying collection under certain circumstances.

martin
  • 93,354
  • 25
  • 191
  • 226
1

Because this question has been marked as a duplicate of this one, let me answer here.

Check if a type is a union

/**
 * @see https://stackoverflow.com/questions/53953814/typescript-check-if-a-type-is-a-union/53955431
 */
type IsSingleton<T> =
  [T] extends [UnionToIntersection<T>]
    ? true
    : false

/**
 * @author https://stackoverflow.com/users/2887218/jcalz
 * @see https://stackoverflow.com/a/50375286/10325032
 */
type UnionToIntersection<Union> =
  (Union extends any
    ? (argument: Union) => void
    : never
  ) extends (argument: infer Intersection) => void
    ? Intersection
  : never;

Allow only singleton types

type SingletonOnly<T> =
  IsSingleton<T> extends true
    ? T
    : never

Restrict a function to accept only a singleton type

declare function foo<K extends string>(s: SingletonOnly<K>): void

declare const singleton: 'foo';
foo(singleton);

declare const union: "foo" | "bar";
foo(union); // Compile-time error

TypeScript Playground

Karol Majewski
  • 23,596
  • 8
  • 44
  • 53
0

You can achieve this with Tuples but these are basically arrays and NOT "objects", so this is not a direct answer for the specific question but instead is intended to give an alternative yet easy approach to the problem as long as keeping the object structure is not required.

A tuple type is another sort of Array type that knows exactly how many elements it contains, and exactly which types it contains at specific positions.

function doSomething(pair: [string, number]) {
  const c = pair[2];
  Error: Tuple type '[string, number]' of length '2' has no element at index '2'.

  // you can also destructure the input to named variables
  const [key, val] = pair;
}
Addis
  • 2,480
  • 2
  • 13
  • 21
0

This is the N pairs type check function so it works not only with single key-value pair but also with any number of pairs.

Code

/**********
 * Type
 ***********/
// REF: https://github.com/type-challenges/type-challenges/issues/2988
type ObjectEntries<T, U = Required<T>> = {
  [K in keyof U]:[K,U[K]]
}[keyof U]

// REF: https://github.com/microsoft/TypeScript/issues/13298#issuecomment-885980381
type UnionToIntersection<U> = (
  U extends never ? never : (arg: U) => never
) extends (arg: infer I) => void
  ? I
  : never;

type UnionToTuple<T> = UnionToIntersection<
  T extends never ? never : (t: T) => T
> extends (_: never) => infer W
  ? [...UnionToTuple<Exclude<T, W>>, W]
  : [];

type Length<T extends unknown[]> = T['length']

type NPairRecord<T = Record<string, any>, N = -1> = Length<UnionToTuple<ObjectEntries<T>>> extends N ? T : never;

/**********
 * Type Checker
 ***********/
function asZeroPair<T>(_: NPairRecord<T, 0>) {}
function asOnePair<T>(_: NPairRecord<T, 1>) {}
function asTwoPair<T>(_: NPairRecord<T, 2>) {}

/**********
 * Example
 ***********/
{
    const obj = {};
    asZeroPair(obj); // 
    asOnePair(obj);  // type error
    asTwoPair(obj);  // type error
}

{
    const obj = {a: 1};
    asZeroPair(obj); // type error
    asOnePair(obj);  // 
    asTwoPair(obj);  // type error
}

{
    const obj = {a: 1, b: 2};
    asZeroPair(obj); // type error
    asOnePair(obj);  // type error
    asTwoPair(obj);  // 
}

Outline

  1. ObjectEntries
    • e.g) {a: 1, b: 2} => ["a", 1] | ["b", 2]
  2. UnionToTuple
    • e.g) ["a", 1] | ["b", 2] => [["a", 1], ["b", 2]]
  3. Check the length with Conditional Types
    • e.g) [["a", 1], ["b", 2]] => length is 2, and check if it's expected.

Playground

Playground

snamiki1212
  • 3
  • 1
  • 2