#Buffer.from with union type - BROKEN

5 messages · Page 1 of 1 (latest)

umbral flicker
#

this is a bug right?

mellow pantherBOT
#
m.a.t.t.t#0

Preview:```ts
import {Buffer} from "node:buffer"

function hash(input: string | Buffer) {
const data = Buffer.from(input)
console.log(data)
}

function hashhhh(input: string | Buffer) {
let data: Buffer
if (typeof input === "string") {
data = Buffer.f
...```

umbral flicker
#

to fix... i've had to use this smdh

// pray to the spring of typescript
const data = typeof input === 'string'
  ? Buffer.from(input) : Buffer.from(input)
vagrant minnow
#

!*:overload-combinations

mellow pantherBOT
#
retsam19#0
`!retsam19:overload-combinations`:

When calling an overloaded function, TS will not match calls that are 'combinations' of signatures other signatures:

// 'public' signatures
function foo(x: string): string;
function foo(x: number): number;
// 'private' implementation signature
function foo(x: string | number) { return x; }

declare const x: string | number;
foo(x)
//  ^
// No overload matches this call.

In theory, TS could allow this: foo has a signature that accepts string and foo has a signature that accepts number, so it could allow string | number, but in practice it doesn't.

It's much less clear how this should work for more complicated cases with more arguments, and it would be potentially very expensive for TS to attempt to calculate these - easy to do if there's two signatures, but there can be many signatures.

For details see #1805, #14107