Bill Noble
Bill Noble

Reputation: 6744

Typescript assignment causes compilation error

Typescript is giving me a compilation error with the following code:

console.log('YHISTORY:login: data = '+data);
let theData = JSON.parse(data);
console.log('YHISTORY:login: theData = '+JSON.stringify(theData));

It complains when compiling the second line as follows:

Error TS2345: Argument of type '{}' is not assignable to parameter of type 'string'

When I run this code the first line generates this console output:

YHISTORY:login: data = {"key":"4ba9f2eec3a59e76799a728f0dcda831b7f1fb66"}

The third line does not produce any console output. The following error is generated:

EXCEPTION: Error: Uncaught (in promise): TypeError: this is null

Upvotes: 0

Views: 400

Answers (1)

SergeyK
SergeyK

Reputation: 1577

The error is not about assignment, it's about calling JSON.parse() function. Error looks like your data variable is already an object, when string is expected as function argument.

You can reproduce this error with this example:

var data = {};
JSON.parse(data);

Upvotes: 1

Related Questions