Why does ts-node define an options parameter as null | undefined as in fs.readFileSync?
Asked Answered
K

1

0

I am confused with fs.t.ds definition for readFileSync, one of them are defined as

export function readFileSync(
    path: PathOrFileDescriptor,
    options?: {
        encoding?: null | undefined;
        flag?: string | undefined;
    } | null
): Buffer;

Why do they define the optional property encoding as encoding?: null | undefined;?

I have a function that calls fs.readFileSync(), how do I type my function parameter to call fs.readFileSync?

PS. Difference between TypeScript optional type and type | undefined and Can an optional property value be null? give me some clues but I still do not fully understand it.

Koester answered 6/9, 2023 at 15:16 Comment(3)
encoding?: null | undefined; is equivalent to encoding?: null;, at least in modern TS (see #53808017 ), I don't know if things were ever different in the past. Does that help? how do I type my function parameter to call fs.readFileSync? Depends how the parameter is passed into readFileSync, an example (of how you want to implement it in plain JS) would help clarifyBenford
Thanks for the comment. As for my second question, I have coded encoding: BufferEncoding to make it work. I had thought to delete my second but since I got an answer for that I keep it. Now I am only interested in my first question, i.e. why define an optional property as type null | undefined; I feel undefined part is unnecessary. But you seem to suggest that is for historical reason ?Koester
That's only a guess. It may have served some point earlier (jcalz would probably know), or it may well just have been something the person who wrote this code did not even think about and just wrote encoding?: null | undefined;, which works and is type-safe, despite being a bit redundant.Benford
C
0

readFileSync has multiple definition (function overloading), if you are using VSCode, you could try to click readFileSync while holding Ctrl key, and it will shows all definitions.

I guess the definition you want is:

export function readFileSync(
    path: PathOrFileDescriptor,
    options:
        | {
              encoding: BufferEncoding;
              flag?: string | undefined;
          }
        | BufferEncoding
): string;

so just type your interface like this:

interface FOptions {
  encoding: BufferEncoding;
  flag?: string | undefined;
}

In FOptions interface you provided, the encoding property is defined as string, which is widens than BufferEncoding (fixed string union), so the definition you want is skipped. That's why the compiler try to use the other definition

Confessedly answered 6/9, 2023 at 16:2 Comment(2)
Thanks for answering my question. Actually, I have figured that out. But I still don't understand why they define an optional property as null | undefined.Koester
the point of that overloading is the return type, it shows that if you didn't provide encoding property or it is null, it will return Buffer instead of stringConfessedly

© 2022 - 2024 — McMap. All rights reserved.