#why am I getting the first overload of fs.readfile??

6 messages · Page 1 of 1 (latest)

spark sentinelBOT
#
ricmed#0

Preview:```ts
import fs from "node:fs"

type CB = (e: unknown, r: unknown) => void

function wrap<
Fn extends (a1: A1, a2: A2, cb: CB) => void,
A1 = unknown,
A2 = unknown

(fn: Fn, a1: A1, a2: A2) {
fn(a1, a2, (_err, file) => {})
}

function _main() {
const res = wrap(fs.readFile, "foo.txt", {
encoding: "utf8",
})
...```

old tendon
#

why am I getting the first overload of fs.readfile??

old tendon
#

!helper

full tinsel
#

the { encoding: 'utf8' } parameter is getting its type inferred as { encoding: string }, which isn't compatible with any of readFile's singatures. if you slap an as const after it to infer the string as a literal type then it works:

spark sentinelBOT
#
mkantor#0

Preview:ts ... function _main() { const res = wrap(fs.readFile, "foo.txt", { encoding: "utf8" as const, }) }

old tendon
#

got it. Thanks! @full tinsel