Code Monkey home page Code Monkey logo

rats's Introduction

Rats!

Where TypeScript doesn't save you, or doesn't do what you want.

Lossy type downcasting

playground

let t: { a: number } = { a: 1 };
let u: {} = t;
let v: { a?: string } = u;

if (v.a !== undefined) {
    alert(v.a.toUpperCase()); // boom!
}

This typechecks, but because TS does not require verification of downcast value's shape, this code blows up.

Infinity is not of type Infinity

playground

type Inf = 178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123721178312789361972639173621978361987263912736123;

const n: Inf = Infinity;

The giant number on the RHS of Inf evaluates to Infinity in a JS interpreter, so TS types Inf as Infinity (verify by hovering over the type alias).

But the Infinity value is not assignable to a value of type Inf because the Infinity value is a number type. Makes sense.

Uninitialized self-reference not caught in IIFE

playground

let c: any = (() => c)();

TypeScript thinks c is in scope of the IIFE, but in fact is not initialized until after the IIFE is evaluated. At runtime, this throws a reference error.

Numbers permitted in string index type

playground

const map: {[k: string]: string} = {
  1: '',
}

This typechecks, but const map: {[k: number]: string} = {'one': ''} wouldn't. The runtime types are semantically correct because object properties are strings, but accepting a number where strings are expected isn't desirable during development.

rats's People

Contributors

ayazhafiz avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.