Const assertion "as const"
Khi khai báo biến, thêm as const vào cuối sẽ làm cho giá trị đó thành readonly và chuyển thành literal type.
Với giá trị primitive type thì không cảm nhận được nhiều lợi ích, nhưng với array hoặc object literal thì rất tiện lợi.
tsconststr1 = "hello";conststr2 = "hello" asconst ; // Trường hợp này có hay không as const cũng giống nhauconstarray1 = [1, 2, 3];constarray2 = [1, 2, 3] asconst ;constobj1 = {name : "pikachu",no : 25,genre : "mouse pokémon",height : 0.4,weight : 6.0,};constobj2 = {name : "pikachu",no : 25,genre : "mouse pokémon",height : 0.4,weight : 6.0,} asconst ;
tsconststr1 = "hello";conststr2 = "hello" asconst ; // Trường hợp này có hay không as const cũng giống nhauconstarray1 = [1, 2, 3];constarray2 = [1, 2, 3] asconst ;constobj1 = {name : "pikachu",no : 25,genre : "mouse pokémon",height : 0.4,weight : 6.0,};constobj2 = {name : "pikachu",no : 25,genre : "mouse pokémon",height : 0.4,weight : 6.0,} asconst ;
Vì trở thành readonly nên đương nhiên không thể gán giá trị mới.
tsarray1 [0] = 4;Cannot assign to '0' because it is a read-only property.2540Cannot assign to '0' because it is a read-only property.array2 [0 ] = 4;obj1 .name = "raichu";Cannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.obj2 .= "raichu"; name
tsarray1 [0] = 4;Cannot assign to '0' because it is a read-only property.2540Cannot assign to '0' because it is a read-only property.array2 [0 ] = 4;obj1 .name = "raichu";Cannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.obj2 .= "raichu"; name
Sự khác biệt giữa readonly và const assertion
Cả hai đều có chức năng làm cho property của object thành readonly, nhưng có các điểm khác biệt sau.
readonly có thể áp dụng cho từng property
const assertion là khai báo cho toàn bộ object nên tất cả property đều bị ảnh hưởng, trong khi readonly có thể áp dụng chỉ cho những property cần thiết.
const assertion có thể áp dụng readonly đệ quy
Hành vi khác nhau khi có object lồng trong object. Ví dụ, giả sử có object như sau.
tstypeCountry = {name : string;capitalCity : string;};typeContinent = {readonlyname : string;readonlycanada :Country ;readonlyus :Country ;readonlymexico :Country ;};constamerica :Continent = {name : "North American Continent",canada : {name : "Republic of Canada",capitalCity : "Ottawa",},us : {name : "United States of America",capitalCity : "Washington, D.C.",},mexico : {name : "United Mexican States",capitalCity : "Mexico City",},};
tstypeCountry = {name : string;capitalCity : string;};typeContinent = {readonlyname : string;readonlycanada :Country ;readonlyus :Country ;readonlymexico :Country ;};constamerica :Continent = {name : "North American Continent",canada : {name : "Republic of Canada",capitalCity : "Ottawa",},us : {name : "United States of America",capitalCity : "Washington, D.C.",},mexico : {name : "United Mexican States",capitalCity : "Mexico City",},};
Ở đây tất cả property của type alias Continent đều là readonly. Do đó không thể làm những việc như sau.
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.america .= "African Continent"; name Cannot assign to 'canada' because it is a read-only property.2540Cannot assign to 'canada' because it is a read-only property.america .= { canada name : "Republic of Côte d'Ivoire",capitalCity : "Yamoussoukro",};
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.america .= "African Continent"; name Cannot assign to 'canada' because it is a read-only property.2540Cannot assign to 'canada' because it is a read-only property.america .= { canada name : "Republic of Côte d'Ivoire",capitalCity : "Yamoussoukro",};
Tuy nhiên, những việc như sau vẫn có thể thực hiện được mà không có vấn đề.
tsamerica .canada .name = "Republic of Côte d'Ivoire";america .canada .capitalCity = "Yamoussoukro";
tsamerica .canada .name = "Republic of Côte d'Ivoire";america .canada .capitalCity = "Yamoussoukro";
Điều này là do khi property có readonly là object, thì property của object đó không được làm thành readonly.
const assertion cố định tất cả property
Thêm as const.
tsconstamerica = {name : "North American Continent",canada : {name : "Republic of Canada",capitalCity : "Ottawa",},us : {name : "United States of America",capitalCity : "Washington, D.C.",},mexico : {name : "United Mexican States",capitalCity : "Mexico City",},} asconst ;
tsconstamerica = {name : "North American Continent",canada : {name : "Republic of Canada",capitalCity : "Ottawa",},us : {name : "United States of America",capitalCity : "Washington, D.C.",},mexico : {name : "United Mexican States",capitalCity : "Mexico City",},} asconst ;
Tương tự như readonly, không thể gán cho property ở top-level.
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.america .= "African Continent"; name Cannot assign to 'canada' because it is a read-only property.2540Cannot assign to 'canada' because it is a read-only property.america .= { canada name : "Republic of Côte d'Ivoire",capitalCity : "Yamoussoukro",};
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.america .= "African Continent"; name Cannot assign to 'canada' because it is a read-only property.2540Cannot assign to 'canada' because it is a read-only property.america .= { canada name : "Republic of Côte d'Ivoire",capitalCity : "Yamoussoukro",};
Không chỉ vậy, property của object con cũng được làm thành readonly.
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.america .canada .= "Republic of Côte d'Ivoire"; name Cannot assign to 'capitalCity' because it is a read-only property.2540Cannot assign to 'capitalCity' because it is a read-only property.america .canada .= "Yamoussoukro"; capitalCity
tsCannot assign to 'name' because it is a read-only property.2540Cannot assign to 'name' because it is a read-only property.america .canada .= "Republic of Côte d'Ivoire"; name Cannot assign to 'capitalCity' because it is a read-only property.2540Cannot assign to 'capitalCity' because it is a read-only property.america .canada .= "Yamoussoukro"; capitalCity
Thông tin liên quan
📄️ Type assertion "as"
TypeScript có tính năng ghi đè type inference. Tính năng này được gọi là type assertion.
📄️ Readonly property của object type
Trong TypeScript, có thể đặt property của object thành read-only. Thêm modifier readonly vào property muốn đặt thành read-only. Khi cố gán giá trị cho property read-only, TypeScript compiler sẽ cảnh báo không thể gán.