Search code examples
typescript

TypeScript: object with any keys except one


I just want a type like this one:

type ObjectWithAnyKey = { [key: string]: string };

with all keys allowed except a foo key.


Solution

  • I think in this case you can use the following definition:

    type ObjectWithAnyKeyExceptFoo = {
      [key: string]: string
    } & { foo?: never };
    

    The type {foo?: never} has an optional property named foo, whose type is never (which actually is the same as undefined for optional properties). So if you have a property named foo, it can't have a defined value. And actually since undefined & string is never, you can't have a foo property at all. Let's make sure it works:

    function acceptAnyKeyAcceptFoo(obj: ObjectWithAnyKeyExceptFoo) { }
    
    acceptAnyKeyAcceptFoo({}); // okay
    acceptAnyKeyAcceptFoo({ a: "123" }); // okay
    acceptAnyKeyAcceptFoo({ a: "123", foo: "oops" }); // error!
    //  ----------------------------> ~~~
    // Type 'string' is not assignable to type 'undefined'.
    acceptAnyKeyAcceptFoo({ a: "123", foo: undefined }); // error!
    //  ----------------> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Type 'undefined' is not assignable to type 'string'.
    

    Looks good.


    More technical stuff below:

    Note that using the intersection (&) here is a little strange, and maybe even cheating a little bit. With an index signature, manually specified properties like foo are required to have values that are assignable to their index signature property type also. The following doesn't work because it violates that:

    type BadDefn = {
      [key: string]: string;
      foo?: never; // error!
    //~~~ <-- undefined is not assignable to string
    }
    

    Since foo may be undefined, but string is not, the compiler is unhappy. Never mind that most values from the index signature actually will be undefined and not string (e.g., const obj: ObjectWithAnyKeyExceptFoo = {a: "123"} has an a property of type string, but what if you access its b property? You will get undefined at runtime, but the compiler says string). But that's the way index signatures are, I guess.

    The intersection definition is essentially the same thing as the disallowed type above, except that it sidesteps the compiler error. There are often issues with doing this intentionally to violate the index signature (see this question), but we don't actually want to use that violating property. We don't want foo properties at all, and since the compiler sees the foo property as string & undefined, which is never, it's actually better for this use case.

    Non-violating single types like these can be made:

    type OkayDefnButPossiblyUndefined = {
      [key: string]: string | undefined;
      foo?: never;
    }
    

    This one is actually reasonable if you want to represent that obj.b is undefined and not string, but might not be the best fit if you like the current index signature behavior. There's also this one:

    type AlsoOkayButRequiresFoo = {
      [key: string]: string;
      foo: never;
    }
    

    which is worse, since foo becomes a required property and it's hard to actually initialize something of this type.

    End technical stuff


    Okay, hope that helps. Good luck!

    Link to code