tsutils
tsutils copied to clipboard
isPropertyReadonlyInType false-positive for rest/spread types
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
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.
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.