122

I have a simple function that takes a function as it's argument and returns a new function. I get Object is of type 'unknown' when calling the returned function

const makeFunction = <T>(callback: (someParam: number, options: T) => any) => {

  return (options: T) => {
    const param = 4;

    return callback(param, options)  
  }  
}

Above code is okay for typescript but when I am calling function I get complaint

makeFunction((param, options) => {
  const a = options.optionsValue //(parameter) options: unknown,  Object is of type 'unknown'
})({optionsValue: 'some value'})
0

13 Answers 13

175

Since this is the first result you get when you google Object is of type 'unknown', I want to post my case. It might help future readers. This is not the answer to OP's question.

I got this error in the catch block. After debugging for a while I came to know that starting typescript v4.0, catch clause variables have type unknown instead of any.

And according to docs:

unknown is safer than any because it reminds us that we need to perform some sort of type-checks before operating on our values.

My code looked something like this before v4.0:

try {
  // try something exceptional here
} catch (error) {
  console.log(error.message);
}

And to fix this error, I had to put an additional if check on error variable.

try {
  // try something exceptional here
} catch (error) {
  let errorMessage = "Failed to do something exceptional";
  if (error instanceof Error) {
    errorMessage = error.message;
  }
  console.log(errorMessage);
}
Sign up to request clarification or add additional context in comments.

6 Comments

And if you updated Typescript, and simply want the linter to stop complaining... just do catch(error: any) explicitly and narrow it down yourself as-needed. I realize some frown upon this, but my app has various API libraries which throw very different error objects and I will narrow things down as I find time.
simplifying console.log({ error: err instanceof Error ? err.message : "Failed to do something exceptional" })
Is it safe to use instanceof Error if you have minifier?
This works if you are accessing some property that exists on Error, like message, but what if I need to access some property like code ( eg: err.code) that is thrown by fs?
@BradGreens if you do that, you get Catch clause variable cannot have a type annotation
|
118

updating my tsconfig.json with the following has worked for me:

"useUnknownInCatchVariables": false,

Update:

you need to put it like this under compilerOptions

"compilerOptions": {
    "useUnknownInCatchVariables": false
  }

Note: You need Typescript v4.4 or higher to be able to use this compiler option else you get a compiler error.

npm install -g ts-node@latest

npm install -g typescript@latest

2 Comments

You need Typescript v4.4 or higher to be able to use this compiler option else you get a compiler error. So if you are using TS version less than 4.4 and want this quick fix, first you'll need to update the TS version to >= 4.4.
57

I got this issue in a try/catch bumping the Typescript to version 4.4 and found in the documentation the explanation for that.

In resume they added the flag useUnknownInCatchVariables that is true by default if strict. It basically changes the type of the error in a catch from any to unknown causing this issue.

So in order to bump to 4.4 you have some options:

try {
    ...
} catch(e) {
    console.log((e as Error).message)
}

or:

try {
    ...
} catch(e) {
    if (e instanceof Error) {
        console.log(e.message)
    }
}

or in your tsconfig.json you can explicitly set the flag to false:

{
    "compilerOptions": {
        "strict": true,
        "useUnknownInCatchVariables": false
    }
}

Comments

26

You can use casting: (err as Error)

I like the if statement solution, but this also works:

catch (error) {
  let msg = (error as Error).message;
}

Comments

18

We need to think how TS can infer the type from this definition. TS can understand type from two places:

  • explicit generic type set
  • type of second argument of the function

In your use case you don't provide type in any of those places and this is the reason you get unknown, because how TS can know what argument type you need. In order to give TS possibility to understand the type you can do or:

Set explicitly generic by:

makeFunction<YourType>((param, options) => {...))

Set type in callback function by for example defining one beforehand:

const f = (a: number, b: {a: string}) => b // here types are set
makeFunction(f)({a: 'some value'}) // makeFunction is able to infer the types by f

You can also do this inline by saying ((param: number, options: MyType))

Answer after comment if options can be dynamic

I believe you want below behavior:

const makeFunction = <F extends (someParam: number, options: any) => any>(callback: F) => {

  return (options: Parameters<F>[1]) => {
    const param = 4;

    return callback(param, options)  
  }  
}
const f = (a: number, b: {a: string}) => b
makeFunction(f)({ a: 'a' })
const g = (a: number, b: {b: number}) => b
makeFunction(g)({b: 1})

We say few things:

  • F is now function which extends from binary function, and we directly infer its type
  • Parameters<F>[1] is second argument type of given function F type

1 Comment

Thank you for your answer, but what about if i want options as dynamic type? its object but object properties can be different depends on where it used
14

Issue with TS 4.0 explained here: https://devblogs.microsoft.com/typescript/announcing-typescript-4-0/#unknown-on-catch

All of the other answers didn't work for me but using the isAxiosError worked just fine:

   } catch (error) {
      if (axios.isAxiosError(error)) {
        const errResp = error.response;
        // Handle your error type safe here
      } else {
        // Handle the unknown
      }
    }

No Object is of type 'unknown'.ts(2571) error anymore.

1 Comment

the original question doesn't mention axios, but it was exactly what i needed.
3

Create an interface for options or use any as type,

makeFunction((param, options: any) => {

  const a = options.optionsValue;

  })({optionsValue: 'some value'});

Typescript takes options type as {} which causes issue during compilation.

Comments

2

Add a type to the calling function like this:

function(): Observable<any>

to avoid it from returnig unknown.

Comments

1

You have 2 options:

  • Verify the type by doing
try {
 
} catch (error) {
  if (error instanceof Error) {
    console.error(error.message);
  } else {
    console.error('An unknown error occurred');
  }
}
  • Force the error
try {
} catch (error) {
  const err = error as Error;
  console.error(err.message);
}

Comments

0

This I have used, this will allow you to do gridObj., custom properties

let gridobj : any = this.$refs.grid;
gridobj.columns(col, "add");

Comments

0

I stumbled on this while coding in Angular, and it occurs within the *ngFor loop using the keyvalue pipe. In such loops, if the value is an object, TypeScript needs to know the type of the key and the value. So when initializing the object you're going to be iterating through using *ngFor, be certain to precise what type is expected of the key and what type is expected of the value, like so:

some_object: {[key: number]: string} = {2: 'foo', 1: 'bar'};

Comments

0

I took such as error while useState. I describe for error usestate:

'''`const[error, setError]=useState(String);`

and as for that catch block

catch (ex) {
        let erMssg = "exceptional";
        if (ex instanceof Error) {
            erMssg  = ex.message;
          }
        setError(erMssg );
        setLoading(false);
    }

Comments

-1

simply put "any" after the catch error message, it should work.

`try {

} catch (error: any) {

console.log(error.message); }`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.