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.
encoding?: null | undefined;
is equivalent toencoding?: 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 intoreadFileSync
, an example (of how you want to implement it in plain JS) would help clarify – Benfordencoding: 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 typenull | undefined;
I feelundefined
part is unnecessary. But you seem to suggest that is for historical reason ? – Koesterencoding?: null | undefined;
, which works and is type-safe, despite being a bit redundant. – Benford