import { get , getOrUndefined } from "./get" ; describe ( "get" , ( ) => { it ( "provides a safe alternative to array subscript access" , ( ) => { const xs = [ 1 , 2 , 3 ] as const ; expect < 1 > ( get ( xs , 0 ) ) . toBe ( 1 ) ; expect < 2 > ( get ( xs , 1 ) ) . toBe ( 2 ) ; expect < 3 > ( get ( xs , 2 ) ) . toBe ( 3 ) ; get ( xs , 100 ) ; get ( xs , - 1 ) ; get ( xs , "length" ) ; const ys = [ 1 , 2 , 3 ] ; expect < number | undefined > ( get ( ys , 1 ) ) . toBe ( 2 ) ; expect < number | undefined > ( get ( ys , 100 ) ) . toBe ( undefined ) ; expect < number | undefined > ( get ( ys , - 1 ) ) . toBe ( undefined ) ; const zs = [ 1 , , 2 , 3 ] ; expect < number | undefined > ( get ( zs , 1 ) ) . toBe ( undefined ) ; expect < number | undefined > ( get ( zs , 100 ) ) . toBe ( undefined ) ; expect < number | undefined > ( get ( zs , - 1 ) ) . toBe ( undefined ) ; const as = [ 1 , 2 , 3 ] ; expect < 1 | 2 | 3 | undefined > ( get ( as , 1 ) ) . toBe ( 2 ) ; expect < 1 | 2 | 3 | undefined > ( get ( as , 100 ) ) . toBe ( undefined ) ; expect < 1 | 2 | 3 | undefined > ( get ( as , - 1 ) ) . toBe ( undefined ) ; const bs = [ 1 , "a" , "b" ] ; expect < number | undefined > ( get ( bs , 0 ) ) . toBe ( 1 ) ; expect < string | undefined > ( get ( bs , 1 ) ) . toBe ( "a" ) ; expect < string | undefined > ( get ( bs , 100 ) ) . toBe ( undefined ) ; expect < string | number | undefined > ( get ( bs , - 1 ) ) . toBe ( undefined ) ; get ( bs , "length" ) ; const record = { 1 : "asdf" } ; expect < string | undefined > ( get ( record , 1 ) ) . toBe ( "asdf" ) ; expect < string | undefined > ( get ( record , 100 ) ) . toBe ( undefined ) ; const obj = { 1 : "asdf" } ; expect < string > ( get ( obj , 1 ) ) . toBe ( "asdf" ) ; get ( obj , 100 ) ; const constObj = { 1 : "asdf" } as const ; expect < "asdf" > ( get ( constObj , 1 ) ) . toBe ( "asdf" ) ; get ( constObj , 100 ) ; const str = "foo" as const ; expect < string | undefined > ( get ( str , 1 ) ) . toBe ( "o" ) ; } ) ; } ) ; describe ( "getOrUndefined" , ( ) => { it ( "provides a safe alternative to array subscript access" , ( ) => { const xs = [ 1 , 2 , 3 ] ; expect < number | undefined > ( getOrUndefined ( xs , 1 ) ) . toBe ( 2 ) ; expect < number | undefined > ( getOrUndefined ( xs , 100 ) ) . toBe ( undefined ) ; expect < number | undefined > ( getOrUndefined ( xs , - 1 ) ) . toBe ( undefined ) ; } ) ; } ) ;