Search code examples
jsontypescripteslinttype-assertion

How to hint the type of a function I do not control?


When parsing a JSON-formatted string I get a linter error:

let mqttMessage = JSON.parse(message.toString())

// ESLint: Unsafe assignment of an `any` value. (@typescript-eslint/no-unsafe-assignment)

I control the content of message so I would like to tell TS that what comes out of JSON.parse() is actually an Object. How can I do that?

Note: I could silence the warning, but I would like to understand if there is a better way to approach the problem.


Solution

  • The problem is that JSON.parse returns an any type.

    That's fair enough right - TypeScript doesn't know if it's going to parse out to a string, a number, or an object.

    You have a linting rule saying 'Don't allow assigning variables as any'.

    So yeah, you could coerce the result of your JSON.parse

    type SomeObjectIKnowAbout = {
    
    }; 
    const result = JSON.parse(message.toString()) as SomeObjectIKnowAbout; 
    

    What I tend to like doing in this scenario is create a specific parsing function, that will assert at runtime that the obj really is of the shape you are saying, and will do the type casting to you can treat it while you're writing your code as that object.

    type SomeObjectIKnowAbout = {
        userId: string; 
    }
    
    type ToStringable = {
        toString: () => string; 
    }
    
    function parseMessage(message: ToStringable ) : SomeObjectIKnowAbout {
        const obj = JSON.parse(message.toString()); //I'm not sure why you are parsing after toStringing tbh. 
    
        if (typeof obj === 'object' && obj.userId && typeof obj.userId === 'string') {
            return obj as SomeObjectIKnowAbout; 
        }
        else {
            throw new Error ("message was not a valid SomeObjectIKnowAbout"); 
        }
    }