develwoutacause’s avatardevelwoutacause’s Twitter Archive—№ 476

  1. In today's #WTF #TypeScript, I present the following snippet, which compiles and runs without error, yet gives the complete wrong answer. Hint: Uint8Array does not extend ArrayBuffer.
    A TypeScript code snippet with the following content:

```
function normalize(input: ArrayBuffer | string): ArrayBuffer {
    if (input instanceof ArrayBuffer) return input;
    return new TextEncoder().encode(input);
}

console.log(normalize(new Uint8Array([ 0, 1, 2, 3 ])));
// Uint8Array(7) [ 48, 44, 49, 44, 50, 44, 51 ] - Wat?
```