tsutils icon indicating copy to clipboard operation
tsutils copied to clipboard

isPropertyReadonlyInType false-positive for rest/spread types

Open ajafff opened this issue 5 years ago • 2 comments
trafficstars

const obj = {const: 1 + 1} as const;
const spreaded = { ...obj };
const constSpreaded = { ...spreaded } as const;
const {...rest} = obj;

obj.const; // readonly
spreaded.const; // writeable
constSpreaded.const; // readonly
rest.const; // writeable

ajafff avatar Jan 01 '20 19:01 ajafff

Note to self: This is only relevant for anonymous types. type.symbol.valueDeclaration can be used to get the node this type originated from. If it's an object literal expression, we only need to look for a const context. If it's an object binding rest, there's definitely no readonly property.

ajafff avatar Jan 01 '20 20:01 ajafff

interface SampleInterface {
  readonly x: unknown;
}

const x: SampleInterface = { x: 1 };

const y = { ...x };

For y: checker.typeToString(type) returns "{ x: unknown; }" which is expected.

However: tsutils.isReadonlyProperty(type, type.getProperty("x").getEscapedName(), checker) returns "true" which is not expected after spread operator.

Can anything be done about this? This issue is 2 years old.

iliubinskii avatar Sep 28 '22 10:09 iliubinskii