noUncheckedIndexedAccess
noUncheckedIndexedAccess là compiler option bắt buộc check undefined khi tham chiếu property của index type hoặc phần tử array.
- Mặc định:
false - Phiên bản thêm vào: 4.1
Giải thích
Evaluate nghiêm ngặt khi truy cập property của object được khai báo bằng index type hoặc array.
📄️ Index signature
Trong TypeScript, có trường hợp muốn chỉ định type của property mà không chỉ định tên field của object. Lúc này có thể sử dụng index signature. Ví dụ, object có tất cả property là type number được type annotation như sau.
tstypeObjectLiteralLike = {en : string;fr : string;it : string;[lang : string]: string;};typeArrayObjectLike = {0: string;1: string;[num : number]: string;};functionlog (s : string): void {console .log (s );}constbutterfly :ObjectLiteralLike = {en : "Butterfly",fr : "Papillon",it : "Farfalla",es : "Mariposa",};constphoneticCodes :ArrayObjectLike = {0: "alpha",1: "bravo",2: "charlie",};
tstypeObjectLiteralLike = {en : string;fr : string;it : string;[lang : string]: string;};typeArrayObjectLike = {0: string;1: string;[num : number]: string;};functionlog (s : string): void {console .log (s );}constbutterfly :ObjectLiteralLike = {en : "Butterfly",fr : "Papillon",it : "Farfalla",es : "Mariposa",};constphoneticCodes :ArrayObjectLike = {0: "alpha",1: "bravo",2: "charlie",};
ObjectLiteralLike, ArrayObjectLike đều được khai báo là type của object có property kiểu string.
tsconstspanish : string =butterfly .es ;constthird : string =phoneticCodes [2];console .log (spanish );console .log (third );
tsconstspanish : string =butterfly .es ;constthird : string =phoneticCodes [2];console .log (spanish );console .log (third );
Khi truy cập property của các object này không đảm bảo type safe hoàn toàn. Khi bật option này sẽ báo lỗi như sau:
tsconstType 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.2322Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.: string = spanish butterfly .es ;constType 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.2322Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.: string = third phoneticCodes [2];
tsconstType 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.2322Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.: string = spanish butterfly .es ;constType 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.2322Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.: string = third phoneticCodes [2];
Như vậy, property không được định nghĩa rõ ràng sẽ được phân tích là union type với undefined:
tsconstspanish : string | undefined =butterfly .es ;constthird : string | undefined =phoneticCodes [2];
tsconstspanish : string | undefined =butterfly .es ;constthird : string | undefined =phoneticCodes [2];
Array khi truy cập bằng index notation sẽ được phân tích là union type với undefined, nhưng for-of, array.forEach() không bị ràng buộc này nên nên cân nhắc sử dụng tích cực:
tsconstphoneticCodes : string[] = ["alpha", "bravo", "charlie"];for (constp ofphoneticCodes ) {// ...}phoneticCodes .forEach ((p : string) => {// ...});
tsconstphoneticCodes : string[] = ["alpha", "bravo", "charlie"];for (constp ofphoneticCodes ) {// ...}phoneticCodes .forEach ((p : string) => {// ...});