TypeScrip: Oh, ese divertido sistema de tipos

Hola, mi nombre es Dmitry Karlovsky y recientemente yo, junto con Artur Mukminov , realicé un taller donde mostré cómo desarrollar tipofunciones complejas a través de pruebas . Son 2 horas de programación de tipo duro. Entonces, a modo de avance, vea un análisis de las curiosidades del sistema de tipografía mecanografiada.













Las relaciones son duras



Es muy fácil verificar si un tipo es un subtipo de otro usando el tipoternario:







type IsAExtendsB = A extends B ? true : false
      
      





Classify, 2 4 :







  • [ A, '<:', B ]



    — A B.
  • [ A, ':>', B ]



    — B A.
  • [ A, '==', B ]



    — ( ).
  • [ A, '!=', B ]



    — .


, Equal Assert, , , . Assert , .







! ..



Object



object



— , , :







type boolean_is_Object = Assert<
    boolean extends Object ? true : false,
    true
>

type boolean_is_not_object = Assert<
    boolean extends object ? true : false,
    false
>
      
      





, , , :







type Object_vs_object = Assert<
    Classify< Object, object >,
    [ Object, '==', object ]
>
      
      





: (, boolean



) (, Object



), — (, object



), — .







, . Object



, object



.









, , — , :







type boolean_is_true_or_false = Assert<
    boolean,
    true | false
>
      
      





:







enum FL4 { Absurd, False, True, Unknown }

type FL4_is_union = Assert<
    FL4,
    | FL4.Absurd | FL4.False | FL4.True | FL4.Unknown
>
      
      





( ):







type Absurd_is_number = Assert<
    Classify< FL4.Absurd, number >,
    [ FL4.Absurd, '==', number ]
>
      
      





:







type Absurd_is_never_wtf = Assert<
    Classify< FL4.Absurd, 0 >,
    [ never, '<:', 0 ]
>
      
      





, , ? , !







type One_is_never_wtf = Assert<
    Classify< FL4.Absurd, 1 >,
    [ FL4.Absurd, ':>', never ]
>
      
      





, , !







, — , :







enum FL3 { Absurd, False, True }

type Absurd_is_not_Absurd = Assert<
    Equal< FL3.Absurd, FL4.Absurd > | false,
    false
>
      
      





, . , , , :







enum HappyDebugging {
    False = "True", 
    True = "False",
}

type True_extends_string = Assert<
    Classify< HappyDebugging.True, string >,
    [ HappyDebugging.True, '<:', string ]
>
      
      





, number



, string



.









, :







  • never



    . , .
  • unknown



    — . . unknown



    .


Pero, ¿qué se avecina junto a ellos? ¡Sí lo es any



!
Por un lado, es completamente intercambiable con unknown



:







type unknown_is_any = Assert<
    unknown,
    any
>
      
      





Pero por otro lado, como el gato de Schrödinger, es un subtipo never



(y como consecuencia, de cualquier otro tipo de do unknown



) y no lo es al mismo tiempo:







type any_maybe_extends_never = Assert<
    any extends never ? true : false,
    true | false
>
      
      





En resumen, any



rompe el fondo en todos los sentidos posibles. El destino de quienes se encuentran con él cara a cara es duro ...













Todo el código del artículo.







¡Felices chicos de depuración!














All Articles