mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-10 01:56:15 +08:00
35 lines
801 B
TypeScript
35 lines
801 B
TypeScript
// Immutable contracts that do not hold: writes that reach the value
|
|
// through methods, aliases and casts, and stores of values that other
|
|
// references can still change.
|
|
import type { Immutable } from "@scriptc/types";
|
|
|
|
class Counter {
|
|
count = 0;
|
|
bump(): void {
|
|
this.count++;
|
|
}
|
|
}
|
|
|
|
const COUNTER: Immutable<Counter> = new Counter();
|
|
|
|
const shared = [1, 2, 3];
|
|
const VIEW: Immutable<number[]> = shared;
|
|
|
|
const ROWS: Immutable<number[][]> = [[1], [2]];
|
|
|
|
const CONFIG: Immutable<{ name: string }> = { name: "x" };
|
|
|
|
function rename(config: { name: string }): void {
|
|
config.name = "y";
|
|
}
|
|
|
|
function grow(rows: number[][]): void {
|
|
rows[0]!.push(4);
|
|
}
|
|
|
|
COUNTER.bump();
|
|
rename(CONFIG);
|
|
grow(ROWS as number[][]);
|
|
shared.push(4);
|
|
console.log(COUNTER.count, VIEW.length, ROWS.length, CONFIG.name);
|