17

Fairly new to Typescript and have this small block of code:

let payload;
if (event.body) {
  payload = JSON.parse(event.body);
}

The payload line is throwing the below error from eslint:

Unsafe assignment of an any value. eslint(@typescript-eslint/no-unsafe-assignment

This is more of an exercise in understanding the intricacies with Typescript and eslint working together, what can I do to resolve this issue? Seems like a minor change would do the trick but I haven't been able to locate a preexisting question with the same intent. Thanks in advance for the help!

3 Answers 3

14

I got it.

let payload: unknown;

Tests pass for eslint as a result.

Sign up to request clarification or add additional context in comments.

Comments

0

Found solution on the https://github.com/typescript-eslint/typescript-eslint/issues/2118

We can use external function to get data with set type of return like:

function(str: string): Superobj {
  return JSON.parse() as Superobj
}

2 Comments

I also found that error if class not found (i.e. file not uploaded to server) so when lint starts it can't see the class that we try to "new" and return error "Unsafe construction of an any type value".
This is not good idea. This gives you false sense that the JSON you got is Superobj. TypeScript will not give you errors but you will get errors in runtime (e.g. trying to access param that is defined in Superobj but actually was missing in the JSON). A better approach is to use type guards or type predicates to verify the data structure before telling TypeScript to assume it's type: More on type predicates here: typescriptlang.org/docs/handbook/2/…
-1

All you need to do is add type assertion.

let payload: IPayload;
if (event.body) {
  payload = JSON.parse(event.body) as IPayload;
}

2 Comments

This is a cast, not an assertion, and won't actually validate anything at runtime. If you want runtime validation that payload is IPayload, you need to add a type guard function, something like function isPayload(input: unknown): input is IPayload {...}. If you use a function like this on the input while assigning it, then Typescript will treat payload as IPayload everywhere it's used.
It is inaccurate to describe as as a cast, and particularly confusing to call it that as it is not the same as cast in other typed languagues. In Typescript, as is a way to do type assertion, and the rest of the comment by @coopereyecat is accurate - it effectively disables much of the type validation and asserts that variable is of that type and as such, it can be a fairly dangerous tool to overuse. It is not strong assertion (as with a type guard function). It's just telling the compiler not to care. See: typescriptlang.org/docs/handbook/2/…

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.