As another answer explains, const doesn't prevent objects from being modified in ES6, it only prevents reassignments.
In order to prevent parameter reassignments globally, TSLint no-parameter-reassignment rule can be used.
In order to prevent object modifications at runtime, Object.freeze should be used. This can be enforced at compilation time with Readonly mapped type. However, this won't have any effect if the types are compatible:
interface compareTo {
(v1: Readonly<Square>, v2: Readonly<Square>): number;
}
const foo: compareTo = (a: Square, b: Square) => {
a.area = 0;
return 1;
}
Generally, this isn't the responsibility of an interface to tell how function should internally work, it just describes its interface, so it would be:
interface compareTo {
(v1: Square, v2: Square): number;
}
const foo: compareTo = (a: Readonly<Square>, b: Readonly<Square>) => {
a.area = 0; // error
return 1;
}
But this will work if types are inferred from generic type, i.e. a type isn't specified in compareTo implementation:
interface compareTo<T = Readonly<Square>> {
(v1: T, v2: T): number;
}
const foo: compareTo = (a, b) => {
a.area = 0; // error
return 1;
}