typescript types fs.readFileSync Как указать кодировку - No overload matches this call
Primary tabs
Для кода вида:
fs.readFileSync(fileName, 'UTF-8')
получаем ошибку:
No overload matches this call.
Overload 1 of 3, '(path: PathOrFileDescriptor, options?: { encoding?: null | undefined; flag?: string | undefined; } | null | undefined): Buffer', gave the following error.
Type '"UTF-8"' has no properties in common with type '{ encoding?: null | undefined; flag?: string | undefined; }'.
Overload 2 of 3, '(path: PathOrFileDescriptor, options: { encoding: BufferEncoding; flag?: string | undefined; } | BufferEncoding): string', gave the following error.
Argument of type '"UTF-8"' is not assignable to parameter of type '{ encoding: BufferEncoding; flag?: string | undefined; } | BufferEncoding'.
Overload 3 of 3, '(path: PathOrFileDescriptor, options?: BufferEncoding | (ObjectEncodingOptions & { flag?: string | undefined; }) | null | undefined): string | Buffer', gave the following error.
Argument of type '"UTF-8"' is not assignable to parameter of type 'BufferEncoding |
Возможное решение
Если полазить по определениям сигнатуры:
................. export function readFileSync( path: PathOrFileDescriptor, options: | { encoding: BufferEncoding; // нужный нам тип flag?: string | undefined; } | BufferEncoding, ): string; ................. // Buffer class type BufferEncoding = | "ascii" | "utf8" | "utf-8" // вот так будет правильно | "utf16le" | "utf-16le" | "ucs2" | "ucs-2" | "base64" | "base64url" | "latin1" | "binary" | "hex";
то можно понять, что нужно просто писать кодировку в нужном регистре, не:
fs.readFileSync(fileName, 'UTF-8')
а:
fs.readFileSync(fileName, 'utf-8')
- Log in to post comments
- 114 reads