const names = ["Alice", "Bob", "Eve"];
names.forEach(function (s) {
console.log(s.toUppercase());
//Property 'toUppercase' does not exist on type 'string'. Did you mean 'toUpperCase'?
});
names.forEach((s) => {
console.log(s.toUppercase());
//Property 'toUppercase' does not exist on type 'string'. Did you mean 'toUpperCase'?
});
๊ฐ์ฒด
let human: {
name: string;
age: number;
};
human = { name: 'ํ๊ธธ๋', age: 13 };
function printId(id: number | string) {
console.log("Your ID is: " + id);
}
// OK
printId(101);
// OK
printId("202");
// ์ค๋ฅ
printId({ myID: 22342 });
function printId(id: number | string) {
console.log(id.toUpperCase());
// Property 'toUpperCase' does not exist on type 'string | number'.
// Property 'toUpperCase' does not exist on type 'number'.
}
typeof๋ฅผ ์ด์ฉํด ๋ถ๊ธฐ ์ฒ๋ฆฌ
function printId(id: number | string) {
if (typeof id === "string") {
// ์ด ๋ถ๊ธฐ์์ id๋ 'string' ํ์ ์ ๊ฐ์ง๋๋ค
console.log(id.toUpperCase());
} else {
// ์ฌ๊ธฐ์์ id๋ 'number' ํ์ ์ ๊ฐ์ง๋๋ค
console.log(id);
}
}
type Point = {
x: number;
y: number;
};
function printCoord(pt: Point) {
console.log("The coordinate's x value is " + pt.x);
console.log("The coordinate's y value is " + pt.y);
}
printCoord({ x: 100, y: 100 });
interface Point {
x: number;
y: number;
}
function printCoord(pt: Point) {
console.log("The coordinate's x value is " + pt.x);
console.log("The coordinate's y value is " + pt.y);
}
printCoord({ x: 100, y: 100 });