. Advertisement .
..3..
. Advertisement .
..4..
I have the example bellow:
const s = 'Some string';
const obj = { ...s };
If I stretch a constant string out into an object, it won’t work. I encounter a typescript error: spread types may only be created from object types.
When I distribute a number type, it also results in an error:
const n = 3;
const obj2 = { ...n };
Please tell me the reason for that issue and the way to fix it. I look forward to receiving your answer. Thank you.
The cause:
The mistake occurs because, in order to use spread syntax with objects, you must additionally give in an object having key-value characteristics that primitive objects like string or integer lack.
Solution:
Just double-check to make sure the passed-in variable is an object whenever you are utilizing spread syntax for objects. As a result, the error: spread types may only be created from object types won’t occur.