skip to Main Content

I have an object inside a large project that uses null as a key and that cannot be changed, since it would break things all over the app.

However, I’m getting Type null cannot be used as an index type. ts(2538) all over the place. Is there a way to tell TypeScript to accept null as an object key the same way it accepts numbers as keys?

demo

3

Answers


  1. In TypeScript, when using an object as an index type, the type system enforces that the index must be assignable to string or number. That’s why you’re getting the error Type null cannot be used as an index type.

    Unfortunately, there is no way to directly tell TypeScript to accept null as a valid index type without modifying the TypeScript compiler or changing the definition of the object.

    However, if modifying the object is not an option, you can work around this issue by using a type assertion (as) to override the type checking temporarily. Here’s an example:

    const MyObject = {
        null: 0,
        a: 1,
        b: 2,
    };
    
    const key: string | null = null;
    
    console.log(MyObject[key as keyof typeof MyObject]); // Use type assertion to bypass type checking
    

    By using key as keyof typeof MyObject, you’re essentially telling TypeScript to treat key as a valid key of the MyObject object. However, keep in mind that this bypasses type checking, so you need to ensure that key is a valid key at runtime; otherwise, you may encounter a runtime error.

    Login or Signup to reply.
  2. All keys in javascript are converted to strings or symbols. Even if assigned non string values, the keys are converted to string.

    So MyObject[null] should become something like MyObject['null'] and should work as is. All you have to do is use ‘null’ (as a string) instead of null when accessing the property.

    Note that there is another change required. The below statement means that key can be assgined any string value or the value null.

    const key: string | null = null;
    

    But MyObject only has three valid keys a,b,null. So you should modify your statement like:

    const key: keyof typeof MyObject  = 'null';
    

    or

    const key  = 'null';
    

    So, key can only be assigned valid MyObject keys.

    const MyObject = {
        null: 0,
        a: 1,
        b: 2,
    }
    
    //const key: keyof typeof MyObject  = 'null';
    const key = 'null';
    
    console.log(MyObject[key]);
    

    Playground

    Login or Signup to reply.
  3. So, in TypeScript they actively decided to only allow the string or number type as index signatures. Probably because most of the time, anyone trying to index into an object using something like null is indicative of an error.

    In your case, you can do something like this:

    function buildInverseMap(source: Array<string | null>) : {[key: string] : number} {
        var inverseMap: { [key: string]: number } = {};
        for (let i = 0; i < source.length; i++) {
            inverseMap[String(source[i])] = i; // coerce to string yourself
        }
        return inverseMap;
    }     // example for function.
    
    inverseMap[source[i] || "null"] = i;  // example for an object. 
    

    Hope that helps! Good luck.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search