In the code below I would expect the find function to return undefined | BufferEncoding
, the same type as the Map has. But undefined | string
is returned. How come?
const chartdetToFsEncodings = new Map<string, BufferEncoding>([
["UTF-8", "utf8"],
["UTF-16LE", "utf16le"],
]);
const supportedEncoding = analyse(buffer)
.map((match) => match.name)
.find((name) =>
chartdetToFsEncodings.get(name)
) as BufferEncoding;
我尝试将Map设置为const,但是出现了一些语法错误。
const chartdetToFsEncodings = new Map<string, BufferEncoding>([
["UTF-8", "utf8"],
["UTF-16LE", "utf16le"],
]) as const;
find
method is used by anstring[]
, so it will returnstring | undefined
. If you want to haveBufferEncoding | undefined
, this is the easiest way:The issue boils down to the incorrect use of
Array.prototype.find
. The method acts like a filter, but returns only the first element in the array for which the predicate returns true. In that case, it can either return an element from the mapped array, from.map(match => match.name)
, which is typed asstring
, orundefined
.If you want to get an array of
BufferEncoding
instead, you can do it directly in.map()
callback: