interface Developer {
name: string;
skill: string;
}
interface Person {
name: string;
age: number;
}
function introduceSelf(): Developer | Person {
return {
name: "darren",
age: 23,
}; // 우리가 예상한 return type은 Person
}
/*
우리가 예상한 return type은 Person 이지만,
intruduceSelf 함수의 return type이
Developer | Person 이므로 공통 특성인 name만 사용할 수 있음.
*/
const daren = introduceSelf();
console.log(daren.age); // Error!
const darren = introduceSelf();
if (darren as Person) { //우리는 darren이 Person이라고 알고 있다.
console.log((darren as Person).age);
}
if( 변수&& typeof 변수 === ‘object’ )
type Fish = { swim: () => void };
type Bird = { fly: () => void };
type Human = { swim?: () => void; fly?: () => void };
function move(animal: Fish | Bird | Human) {
if ("swim" in animal) {
// (parameter) animal: Fish | Human
animal;
} else {
//(parameter) animal: Bird | Human
animal;
}
}
function logValue(x: Date | string) {
if (x instanceof Date) {
console.log(x.toUTCString()); // x:Date
// The toUTCString() method converts a date to a string
} else{
console.log(x.toUpperCase()); // x:string
}
}
logValue(new Date())
logValue('Thu, 09 Jun 2022 15:00:00 GMT')
type Shape = Circle | Square;
function getArea(shape: Shape) {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.sideLength ** 2;
default:
const _exhaustiveCheck: never = shape;
return _exhaustiveCheck;
}
}
// Error 가 발생하는 예제.
interface Triangle {
kind: "triangle";
sideLength: number;
}
type Shape = Circle | Square | Triangle;
function getArea(shape: Shape) {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.sideLength ** 2;
default:
const _exhaustiveCheck: never = shape;
// Type 'Triangle' is not assignable to type 'never'.
return _exhaustiveCheck;
}
}
function isFish(pet: Fish | Bird): pet is Fish {
return (pet as Fish).swim !== undefined; // pet에 swim 매서드가 있는지 확인
}
let pet = getSmallPet(); // pet: Fish | Bird
if (isFish(pet)) {
pet.swim(); // typescript는 항상 pet의 type이 Fish임을 알고 있다.
} else {
pet.fly(); // typescript는 항상 pet의 type이 Bird임을 알고 있다.
}
interface Shape {
kind: "circle" | "square";
radius?: number; // 원에 대한 특성
sideLength?: number; // 사각형에 대한 특성
}
function handleShape(shape: Shape) {
// oops!
if (shape.kind === "rect") {
//This condition will always return 'false' since the types '"circle" | "square"' and '"rect"' have no overlap.
// ...
}
}
function getArea(shape: Shape) {
return Math.PI * shape.radius ** 2;
// Object is possibly 'undefined'.
}
function getArea(shape: Shape) {
if (shape.kind === "circle") {
return Math.PI * shape.radius ** 2;
// Object is possibly 'undefined'.
}
}
function getArea(shape: Shape) {
if (shape.kind === "circle") {
return Math.PI * shape.radius! ** 2;
}
}
// good type define
interface Circle {
kind: "circle";
radius: number;
}
interface Square {
kind: "square";
sideLength: number;
}
type Shape = Circle | Square;
// good
function getArea(shape: Shape) {
if (shape.kind === "circle") {
return Math.PI * shape.radius ** 2;
}
}
// bad
function getArea(shape: Shape) {
return Math.PI * shape.radius ** 2;
/*
(타입스크립트는 Shape이 circle인지 모른다.)
Property 'radius' does not exist on type 'Shape'.
Property 'radius' does not exist on type 'Square'.
*/
}
interface Developer {
name: string;
skill: string;
}
interface Person {
name: string;
age: number;
}
function introduceSelf(): Developer | Person {
return {
name: "darren",
age: 23,
}; // 우리가 예상한 return type은 Person
}
/*
우리가 예상한 return type은 Person 이지만,
intruduceSelf 함수의 return type이
Developer | Person 이므로 공통 특성인 name만 사용할 수 있음.
*/
const daren = introduceSelf();
console.log(daren.age); // Error!
const darren = introduceSelf();
if (darren as Person) { //우리는 darren이 Person이라고 알고 있다.
console.log((darren as Person).age);
}
if( 변수&& typeof 변수 === ‘object’ )
type Fish = { swim: () => void };
type Bird = { fly: () => void };
type Human = { swim?: () => void; fly?: () => void };
function move(animal: Fish | Bird | Human) {
if ("swim" in animal) {
// (parameter) animal: Fish | Human
animal;
} else {
//(parameter) animal: Bird | Human
animal;
}
}
function logValue(x: Date | string) {
if (x instanceof Date) {
console.log(x.toUTCString()); // x:Date
// The toUTCString() method converts a date to a string
} else{
console.log(x.toUpperCase()); // x:string
}
}
logValue(new Date())
logValue('Thu, 09 Jun 2022 15:00:00 GMT')
type Shape = Circle | Square;
function getArea(shape: Shape) {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.sideLength ** 2;
default:
const _exhaustiveCheck: never = shape;
return _exhaustiveCheck;
}
}
// Error 가 발생하는 예제.
interface Triangle {
kind: "triangle";
sideLength: number;
}
type Shape = Circle | Square | Triangle;
function getArea(shape: Shape) {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.sideLength ** 2;
default:
const _exhaustiveCheck: never = shape;
// Type 'Triangle' is not assignable to type 'never'.
return _exhaustiveCheck;
}
}
function isFish(pet: Fish | Bird): pet is Fish {
return (pet as Fish).swim !== undefined; // pet에 swim 매서드가 있는지 확인
}
let pet = getSmallPet(); // pet: Fish | Bird
if (isFish(pet)) {
pet.swim(); // typescript는 항상 pet의 type이 Fish임을 알고 있다.
} else {
pet.fly(); // typescript는 항상 pet의 type이 Bird임을 알고 있다.
}
interface Shape {
kind: "circle" | "square";
radius?: number; // 원에 대한 특성
sideLength?: number; // 사각형에 대한 특성
}
function handleShape(shape: Shape) {
// oops!
if (shape.kind === "rect") {
//This condition will always return 'false' since the types '"circle" | "square"' and '"rect"' have no overlap.
// ...
}
}
function getArea(shape: Shape) {
return Math.PI * shape.radius ** 2;
// Object is possibly 'undefined'.
}
function getArea(shape: Shape) {
if (shape.kind === "circle") {
return Math.PI * shape.radius ** 2;
// Object is possibly 'undefined'.
}
}
function getArea(shape: Shape) {
if (shape.kind === "circle") {
return Math.PI * shape.radius! ** 2;
}
}
// good type define
interface Circle {
kind: "circle";
radius: number;
}
interface Square {
kind: "square";
sideLength: number;
}
type Shape = Circle | Square;
// good
function getArea(shape: Shape) {
if (shape.kind === "circle") {
return Math.PI * shape.radius ** 2;
}
}
// bad
function getArea(shape: Shape) {
return Math.PI * shape.radius ** 2;
/*
(타입스크립트는 Shape이 circle인지 모른다.)
Property 'radius' does not exist on type 'Shape'.
Property 'radius' does not exist on type 'Square'.
*/
}
interface Developer {
name: string;
skill: string;
}
interface Person {
name: string;
age: number;
}
function introduceSelf(): Developer | Person {
return {
name: "darren",
age: 23,
}; // 우리가 예상한 return type은 Person
}
/*
우리가 예상한 return type은 Person 이지만,
intruduceSelf 함수의 return type이
Developer | Person 이므로 공통 특성인 name만 사용할 수 있음.
*/
const daren = introduceSelf();
console.log(daren.age); // Error!
const darren = introduceSelf();
if (darren as Person) { //우리는 darren이 Person이라고 알고 있다.
console.log((darren as Person).age);
}
if( 변수&& typeof 변수 === ‘object’ )
type Fish = { swim: () => void };
type Bird = { fly: () => void };
type Human = { swim?: () => void; fly?: () => void };
function move(animal: Fish | Bird | Human) {
if ("swim" in animal) {
// (parameter) animal: Fish | Human
animal;
} else {
//(parameter) animal: Bird | Human
animal;
}
}
function logValue(x: Date | string) {
if (x instanceof Date) {
console.log(x.toUTCString()); // x:Date
// The toUTCString() method converts a date to a string
} else{
console.log(x.toUpperCase()); // x:string
}
}
logValue(new Date())
logValue('Thu, 09 Jun 2022 15:00:00 GMT')
type Shape = Circle | Square;
function getArea(shape: Shape) {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.sideLength ** 2;
default:
const _exhaustiveCheck: never = shape;
return _exhaustiveCheck;
}
}
// Error 가 발생하는 예제.
interface Triangle {
kind: "triangle";
sideLength: number;
}
type Shape = Circle | Square | Triangle;
function getArea(shape: Shape) {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.sideLength ** 2;
default:
const _exhaustiveCheck: never = shape;
// Type 'Triangle' is not assignable to type 'never'.
return _exhaustiveCheck;
}
}
function isFish(pet: Fish | Bird): pet is Fish {
return (pet as Fish).swim !== undefined; // pet에 swim 매서드가 있는지 확인
}
let pet = getSmallPet(); // pet: Fish | Bird
if (isFish(pet)) {
pet.swim(); // typescript는 항상 pet의 type이 Fish임을 알고 있다.
} else {
pet.fly(); // typescript는 항상 pet의 type이 Bird임을 알고 있다.
}
interface Shape {
kind: "circle" | "square";
radius?: number; // 원에 대한 특성
sideLength?: number; // 사각형에 대한 특성
}
function handleShape(shape: Shape) {
// oops!
if (shape.kind === "rect") {
//This condition will always return 'false' since the types '"circle" | "square"' and '"rect"' have no overlap.
// ...
}
}
function getArea(shape: Shape) {
return Math.PI * shape.radius ** 2;
// Object is possibly 'undefined'.
}
function getArea(shape: Shape) {
if (shape.kind === "circle") {
return Math.PI * shape.radius ** 2;
// Object is possibly 'undefined'.
}
}
function getArea(shape: Shape) {
if (shape.kind === "circle") {
return Math.PI * shape.radius! ** 2;
}
}
// good type define
interface Circle {
kind: "circle";
radius: number;
}
interface Square {
kind: "square";
sideLength: number;
}
type Shape = Circle | Square;
// good
function getArea(shape: Shape) {
if (shape.kind === "circle") {
return Math.PI * shape.radius ** 2;
}
}
// bad
function getArea(shape: Shape) {
return Math.PI * shape.radius ** 2;
/*
(타입스크립트는 Shape이 circle인지 모른다.)
Property 'radius' does not exist on type 'Shape'.
Property 'radius' does not exist on type 'Square'.
*/
}
interface Developer {
name: string;
skill: string;
}
interface Person {
name: string;
age: number;
}
function introduceSelf(): Developer | Person {
return {
name: "darren",
age: 23,
}; // 우리가 예상한 return type은 Person
}
/*
우리가 예상한 return type은 Person 이지만,
intruduceSelf 함수의 return type이
Developer | Person 이므로 공통 특성인 name만 사용할 수 있음.
*/
const daren = introduceSelf();
console.log(daren.age); // Error!
const darren = introduceSelf();
if (darren as Person) { //우리는 darren이 Person이라고 알고 있다.
console.log((darren as Person).age);
}
if( 변수&& typeof 변수 === ‘object’ )
type Fish = { swim: () => void };
type Bird = { fly: () => void };
type Human = { swim?: () => void; fly?: () => void };
function move(animal: Fish | Bird | Human) {
if ("swim" in animal) {
// (parameter) animal: Fish | Human
animal;
} else {
//(parameter) animal: Bird | Human
animal;
}
}
function logValue(x: Date | string) {
if (x instanceof Date) {
console.log(x.toUTCString()); // x:Date
// The toUTCString() method converts a date to a string
} else{
console.log(x.toUpperCase()); // x:string
}
}
logValue(new Date())
logValue('Thu, 09 Jun 2022 15:00:00 GMT')
type Shape = Circle | Square;
function getArea(shape: Shape) {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.sideLength ** 2;
default:
const _exhaustiveCheck: never = shape;
return _exhaustiveCheck;
}
}
// Error 가 발생하는 예제.
interface Triangle {
kind: "triangle";
sideLength: number;
}
type Shape = Circle | Square | Triangle;
function getArea(shape: Shape) {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.sideLength ** 2;
default:
const _exhaustiveCheck: never = shape;
// Type 'Triangle' is not assignable to type 'never'.
return _exhaustiveCheck;
}
}
function isFish(pet: Fish | Bird): pet is Fish {
return (pet as Fish).swim !== undefined; // pet에 swim 매서드가 있는지 확인
}
let pet = getSmallPet(); // pet: Fish | Bird
if (isFish(pet)) {
pet.swim(); // typescript는 항상 pet의 type이 Fish임을 알고 있다.
} else {
pet.fly(); // typescript는 항상 pet의 type이 Bird임을 알고 있다.
}