{meow: () => void}
                  
                  
                   type Dog = {woof: () => void}
                  
                  
                   const createCat = (): Cat => ({meow: () => console.log('Meow!')})
                  
                  
                   const createDog = (): Dog => ({woof: () => console.log('Woof!')})
                  
                  
                   
                  
                  
                   const mutateAnimals = (animals: (Cat | Dog)[]) => animals.push(createDog())
                  
                  
                   
                  
                  
                   const cats: Cat[] = []
                  
                  
                   mutateAnimals(cats)
                  
                  
                   cats.forEach(cat => cat.meow())
                  
                  
                  But failed in runtime with cat.meow is not a function
                  
                  
                  Because of what? Because of mutations!
                  
                  
                  Be smart, don't use mutations
                  
                  
                  
                  
                  
                  2.
                  
                  
                   looks like function overloads are unsafe, this compiles just fine:
                  
                  
                    function fn_(x: string): string;
                  
                  
                   function fn_(x: number): boolean;
                  
                  
                   function fn_(x: string | number): string | boolean {
                  
                  
                     return "oops";
                  
                  
                   }
                  
                  
                   
                  
                  
                   const x = fn_(1);
                  
                  
                      // ^?  const x : boolean
                  
                  
                  
                  
                  
                  
                  
                  
                  In ideal TypeScript code you must avoid function overloads
                  
                  
                  
                  
                  
                  3.
                  
                  
                  Apparently, Record type can break runtime.
                  
                  
                  Below, TS code compiled in strict mode:
                  
                  
                   type A = {str: string}
                  
                  
                   type B = A & {count: number}
                  
                  
                   
                  
                  
                   const b: B = {str: 'str', count: 1}
                  
                  
                   const a: A = b
                  
                  
                   const c: Record<string, string> = a
                  
                  
                   const d: string = c.count
                  
                  
                   
                  
                  
                   console.log(typeof d)
                  
                  
                   // "number"
                  
                  
                  
                  
                  
                  3.1
                  
                  
                  type A = {str: string}
                  
                  
                  type B = A & {count: number}
                  
                  
                  
                  
                  
                  const b: B = {str: 'str', count: 1}
                  
                  
                  const a: A = b
                  
                  
                  
                  
                  
                  interface IRecord{
                  
                  
                    [k: string]: string
                  
                  
                  }
                  
                  
                  
                  
                  
                  //const c: Record<string, string> = a
                  
                  
                  const c: IRecord = a
                  
                  
                  const d: string = c.count
                  
                  
                  
                  
                  
                  console.log(typeof d)
                  
                  
                
СИЛА В СТРАДАНИИ ПЕРЕСЕЧЕНИЕ СЕТИ СМЕРТИ
Но ты же соврал тайпскрипту
А нафига тогда он нужен, если его так легко развести?
Надо писать нормально и всё будет как надо Давайте ещё //@ts-ignore и в прод
Обсуждают сегодня