CPPAlien / JS-QA

前端知识问答
0 stars 0 forks source link

typescript 高级用法 #36

Open CPPAlien opened 4 years ago

CPPAlien commented 4 years ago

-is TypeScript will narrow that variable to that specific type if the original type is compatible

function isFish(pet: Fish | Bird): pet is Fish {
    return (pet as Fish).swim !== undefined;
}

if (isFish(pet)) {
    pet.swim();
}

-Dictionary

interface Dictionary<T> {
  [index: string]: T;
};