doStuff(values: ReadonlyArray<string>) {
// We can read from 'values'...
const copy = values.slice();
console.log(`The first value is ${values[0]}`);
// we can't mutate 'values'. Property 'push' does not exist on type 'readonly string[]'.
// values.push("hello!");
}
push会报错。
ReadonlyArray不能直接构造,可以用普通Array来赋值
const roArray: ReadonlyArray<string> = ["red", "green", "blue"];
另一种写法:
doStuff(values: readonly string[]) {
// We can read from 'values'...
const copy = values.slice();
console.log(`The first value is ${values[0]}`);
// we can't mutate 'values'. Property 'push' does not exist on type 'readonly string[]'.
// values.push("hello!");
}
readonly string[] 不能赋值给普通的string Array