const id = "12-34-56"; fetchProduct(id);
{
const id = 123456; // OK
fetchProductBySerialNumber(id); // OK
}
const v1={
x: 1,
y: 2,
}; // Type is { x: number; y: number; }
const v2={
x: 1 as const,
y: 2,
}; // Type is { x: 1; y: number; }
const v3={
x: 1,
y: 2,
} as const; // Type is { readonly x: 1; readonly y: 2; }
const a1 = [1, 2, 3]; // Type is number[]
const a2 = [1, 2, 3] as const; // Type is readonly [1, 2, 3]
function isInputElement(el: HTMLElement): el is HTMLInputElement {
return 'value' in el;
}
function getElementContent(el: HTMLElement) {
if (isInputElement(el)) {
el; // Type is HTMLInputElement
return el.value;
}
el; // Type is HTMLElement
return el.textContent;
}
const pt = {};
pt.x = 3;
// ~ Property 'x' does not exist on type '{}'
pt.y = 4;
// ~ Property 'y' does not exist on type '{}'
// ts가 새로운 타입을 추론할 수 있게 함.
interface Point { x: number; y: number; }
const pt0 = {};
const pt1 = {...pt0, x: 3};
const pt: Point = {...pt1, y: 4}; // OK
// 삼항 연산자도 사용 가능.
declare let hasMiddle: boolean;
const firstLast = {first: 'Harry', last: 'Truman'};
const president = {...firstLast, ...(hasMiddle ? {middle: 'S'} : {})};
function addOptional<T extends object, U extends object>(
a: T, b: U | null
): T & Partial<U> {
return {...a, ...b};
}
function fn(p: Polygon) { /* ... */ }
polygon.bbox // Type is BoundingBox | undefined
if (polygon.bbox) {
polygon.bbox // Type is BoundingBox fn(polygon);
polygon.bbox // Type is still BoundingBox
}
type Language = 'JavaScript' | 'TypeScript' | 'Python';
function setLanguage(language: Language) { /* ... */ }
setLanguage('JavaScript'); // OK
let language = 'JavaScript'; setLanguage(language);
// ~~~~~~~~ Argument of type 'string' is not assignable
// to parameter of type 'Language'
// 해결 방법
// 1. 타입 선언
let language: Language = 'JavaScript';
setLanguage(language); // OK
// 2. 상수 단언
const language = 'JavaScript';
setLanguage(language); // OK
// 첫 번째 방식이 더 나은듯? 다음과 같은 에러를 보자.
function panTo(where: [number, number]) { /* ... */ }
panTo([10, 20]); // OK
const loc = [10, 20];
panTo(loc);
// ~~~ Argument of type 'number[]' is not assignable to
// parameter of type '[number, number]'
// 해결
const loc: [number, number] = [10, 20];
//or (아마 아래같은 경우는 타입을 직접 지정할 수 없는 경우에 사용하는 걸로 추정)
function panTo(where: readonly [number, number]) { /* ... */ }
const loc = [10, 20] as const;
panTo(loc); // OK
const csvData = "...";
const rawRows = csvData.split('\n');
const headers = rawRows[0].split(',');
const rows = rawRows.slice(1).map(rowStr =>
{ const row = {}; rowStr.split(',').forEach((val, j) => {
row[headers[j]] = val;
});
return row; });
// ~~~~~~~~~~~~~~~ No index signature with a parameter of
// type 'string' was found on type '{}'
// 함수형
const rows = rawRows.slice(1)
.map(rowStr => rowStr.split(',').reduce(
(row, val, i) => (row[headers[i]] = val, row),
// ~~~~~~~~~~~~~~~ No index signature with a parameter of
// type 'string' was found on type '{}'
{}));
// lodash
import _ from 'lodash'; const rows = rawRows.slice(1)
.map(rowStr => _.zipObject(headers, rowStr.split(',')));