const elements = [true, null, undefined, 42];
elements.push('even', ['more']);
// warriors: string[]
const warriors = ['AAA', 'BBB'];
// Ok
warriors.push('CCC');
// Error: Argument of type 'boolean' is not assignable to parameter of type 'string'
warriors.push(true);
let arrayOfNumbers : number[];
arrayOfNumbers = [1, 2, 3, 4, 5];
// 타입 : string 배열을 반환하는 함수
let createStrings: () => string;
// 타입 : string을 반환하는 함수들의 배열
let stringCreators: (() => string)[];
// 타입 : string 또는 number의 배열
let stringOrArrayOfNumbers: string | number[];
// 타입 : 각각 string 또는 number인 요소의 배열
let arrayOfStringOrNumbers: (string | number)[];
// alphabetMaybe: (string | undefined)[]
const alphabetMaybe = ['A', 'B', undefined];
// values: any[]
let values = [];
// values: string[]
values.push('');
// values: (number | string)[]
values[0] = 0;
let arrayOfArrayOfNumbers: number[][];
arrayOfArrayOfNumbers = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
n차원 배열 타입에는 n개의 []가 있습니다.
Type[][]와 (Type[])[]는 같은 타입입니다.
// arrayOfArrayOfNumbers: number[][]
let arrayOfArrayOfNumbers: (number[])[]
const defenders = ['AAA', 'BBB'];
// defender: string
const defender = defenders[0];
const soldiersOrDates = ['AAA', new Date(1782, 6, 3)];
// soldierOrDate: string | Date
const soldierOrDate = soldiersOrDates[0];
function withElements(elements: string[]) {
console.log(elements[9001].length); // Error 없음
}
withElements(['a', 'b']); // 런타임에서 Cannot read property 'length' of undefined' Error 발생함
// soldiers: string[]
const soldiers = ['AAA', 'BBB', 'CCC'];
// soldierAges: number[]
const soldierAges = [21, 31, 41];
// conjoined: (string | number)[]
const conjoined = [...soldiers, ...soldierAges];
function logWarriors(greeting: string, ...names: string[]) {
for (const name of names) {
console.log(`${greeting}, ${name}`);
}
}
const warriors = ['AAA', 'BBB', 'CCC'];
// Ok
logWarriors('hello', warrior);
const birthYears = [1999, 1888, 1777];
// Error: Argument of type 'number' is not assignable to parameter of type 'string'
logWarriors('born in', birthYears);
let YearAndWarrior: [number, string];
// Ok
yearAndWarrior = [1999, 'AA'];
// Error: Type '[number]' is not assignable to type '[number, string]'
yearAndWarriro = [1000]
// year: number
// warrior: string
let [year, warrior] = Math.random() > 0.5 ? [1999, 'AA'] : [1000, 'BB'];
// pairLoose: (boolean | number)[]
const pairLoose = [false, 123];
// Error: Type '(boolean | number)[]' is not assignable to type '[boolean, number]'
// Target requires 2 elements but source may have fewer
const pairTupleLoose: [boolean, number] = pairLoose;
const tupleThree: [boolean, number, string] = [false, 123, 'AAA'];
const tupleTwo: [boolean, number] = [tupleThree[0], tupleThree[1]];
// Error: Type '[boolean, number, string]' is not assignable to type '[boolean, number]'
// Source has 3 elements but target allows only 2
const tupleTwoExtra: [boolean, number] = tupleThree;
function logPair(name: string, value: number) {
console.log(`${name} has ${value}`);
}
const pairArray = ['A', 1];
// Error: A spread argument must either have a tuple type or be passed to a rest parameter
logPair(...pairArray);
const pairTupleIncorrect: [number, string] = [1, 'A'];
// Error: Argument of type 'number' is not assignable to parameter of type 'string'
logPair(...pairTupleIncorrect);
const pairTupleCorrect: [string, number] = ['A', 1];
// Ok
logPair(...pairTupleCorrect);
function firstCharAndSize(input: string) {
// (string | number)[]
return [input[0], input.length];
}
// firstChar: string | number
// size: string | number
const [firstChar, size] = firstCharSize('Gudit');
function firstCharAndSizeExciplit(input: string): [string, number] {
// [string | number]
return [input[0], input.length];
}
// firstChar: string
// size: number
const [firstChar, size] = firstCharSizeExciplit('Gudit');
// unionArray: (number | string)[]
const unionArray = [1157, 'aaa'];
// readonlyTuple: readonly [1157, 'aaa']
const readonlyTuple = [1157, 'aaa'] as const;
const pairMutable: [number, string] = [1157, 'aaa'];
// Ok
pairMutable[0] = 1111;
// Error: The type 'readonly [1157, "aaa"]' is readonly
// and cannot assigned to the mutable type '[number, string]'
const pairAlsoMutable: [number, string] = [1157, 'aaa'] as const;
const pairConst = [1157, 'aaa'] as const;
// Error: Cannot assign to '0' because it is a read-only property
pairConst[0] = 1111;
function firstCharAndSizeAsConst(input: string) {
// readonly [string , number]
return [input[0], input.length] as const;
}
// firstChar: string
// size: number
const [firstChar, size] = firstCharSizeExciplit('Gudit');