issue with HeadingLevel

im currently building a function called createTitleBodyResponse which takes in 4 parameters, one of them being a title_size which corresponds to a HeadingLevel. for some reason, there is an error when i attempt to use this logic. the first implementation has the error
No overload matches this call.
Overload 1 of 3, '(content: string, level?: HeadingLevel.One | undefined): `# ${string}`', gave the following error.
Argument of type 'HeadingLevel' is not assignable to parameter of type 'HeadingLevel.One'.
Overload 2 of 3, '(content: string, level: HeadingLevel.Two): `## ${string}`', gave the following error.
Argument of type 'HeadingLevel' is not assignable to parameter of type 'HeadingLevel.Two'.
Overload 3 of 3, '(content: string, level: HeadingLevel.Three): `### ${string}`', gave the following error.
Argument of type 'HeadingLevel' is not assignable to parameter of type 'HeadingLevel.Three'.ts(2769)
No overload matches this call.
Overload 1 of 3, '(content: string, level?: HeadingLevel.One | undefined): `# ${string}`', gave the following error.
Argument of type 'HeadingLevel' is not assignable to parameter of type 'HeadingLevel.One'.
Overload 2 of 3, '(content: string, level: HeadingLevel.Two): `## ${string}`', gave the following error.
Argument of type 'HeadingLevel' is not assignable to parameter of type 'HeadingLevel.Two'.
Overload 3 of 3, '(content: string, level: HeadingLevel.Three): `### ${string}`', gave the following error.
Argument of type 'HeadingLevel' is not assignable to parameter of type 'HeadingLevel.Three'.ts(2769)
while the error on the second implementation is
No overload matches this call.
Overload 1 of 3, '(content: string, level?: HeadingLevel.One | undefined): `# ${string}`', gave the following error.
Argument of type 'HeadingLevel' is not assignable to parameter of type 'HeadingLevel.One | undefined'.
Type 'HeadingLevel.Two' is not assignable to type 'HeadingLevel.One'.
Overload 2 of 3, '(content: string, level: HeadingLevel.Two): `## ${string}`', gave the following error.
Argument of type 'HeadingLevel' is not assignable to parameter of type 'HeadingLevel.Two'.
Type 'HeadingLevel.One' is not assignable to type 'HeadingLevel.Two'.
Overload 3 of 3, '(content: string, level: HeadingLevel.Three): `### ${string}`', gave the following error.
Argument of type 'HeadingLevel' is not assignable to parameter of type 'HeadingLevel.Three'.
Type 'HeadingLevel.One' is not assignable to type 'HeadingLevel.Three'.ts(2769)
No overload matches this call.
Overload 1 of 3, '(content: string, level?: HeadingLevel.One | undefined): `# ${string}`', gave the following error.
Argument of type 'HeadingLevel' is not assignable to parameter of type 'HeadingLevel.One | undefined'.
Type 'HeadingLevel.Two' is not assignable to type 'HeadingLevel.One'.
Overload 2 of 3, '(content: string, level: HeadingLevel.Two): `## ${string}`', gave the following error.
Argument of type 'HeadingLevel' is not assignable to parameter of type 'HeadingLevel.Two'.
Type 'HeadingLevel.One' is not assignable to type 'HeadingLevel.Two'.
Overload 3 of 3, '(content: string, level: HeadingLevel.Three): `### ${string}`', gave the following error.
Argument of type 'HeadingLevel' is not assignable to parameter of type 'HeadingLevel.Three'.
Type 'HeadingLevel.One' is not assignable to type 'HeadingLevel.Three'.ts(2769)
No description
No description
16 Replies
d.js toolkit
d.js toolkit4w ago
spac3crawler
spac3crawlerOP4w ago
does anybody know how i can fix this? im not even sure if im doing something wrong or if its something to do with the typings
Mafia
Mafia4w ago
I think this is a bug. seems like the underling functions is not being exported correctly heading(content: string, level?: HeadingLevel) You can Ignore the error by using //@ts-expect-error I was able to replicate the error with the following code
import {heading, HeadingLevel} from 'discord.js'

function test(headerType: HeadingLevel) {
return heading('test', headerYype)
}
import {heading, HeadingLevel} from 'discord.js'

function test(headerType: HeadingLevel) {
return heading('test', headerYype)
}
> pnpm ls
dependencies:
discord.js 14.21.0
> pnpm ls
dependencies:
discord.js 14.21.0
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
Mafia
Mafia4w ago
return typing of # {string} vs ## {string} vs ### {string}
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
spac3crawler
spac3crawlerOP4w ago
so is this just a typings issue?
Mafia
Mafia4w ago
Yes, best work around is //@ts-expect-error The underlying function should still work
spac3crawler
spac3crawlerOP4w ago
with either implementation?
Mafia
Mafia4w ago
Yeah, you place it on the line before you call the heading function and it tells the typescript compiler to ignore the error. That should work
spac3crawler
spac3crawlerOP4w ago
so whats the proper way then well from what people are saying its a typings issue so i should use one of those comments for now and wait for it to be fixed? i dont think id know how to fix this myself (as in make a pr) ah i see so then when ts complains, ill know its fixed and i can remove the comment ok that makes sense thank you for the help
ScuttlePeak
ScuttlePeak4w ago
Have you tried using your own formatter function? title_size is undefined or not imported properly You could do something like:
function formatHeading(text: string, level: HeadingLevel): string {
switch (level) {
case HeadingLevel.One: return `**${text}**`;
case HeadingLevel.Two: return `__${text}__`;
case HeadingLevel.Three: return `*${text}*`;
default: return text;
}
}
function formatHeading(text: string, level: HeadingLevel): string {
switch (level) {
case HeadingLevel.One: return `**${text}**`;
case HeadingLevel.Two: return `__${text}__`;
case HeadingLevel.Three: return `*${text}*`;
default: return text;
}
}
And then use:
content: formatHeading(title, title_size),
content: formatHeading(title, title_size),
Amgelo
Amgelo4w ago
the issue is about the djs function though
ScuttlePeak
ScuttlePeak4w ago
Typescripts function overload resolution doesnt work well with unions to my knowledge. He could solve this by using a switch or if statement to narrow the title size before calling heading() I dont think the problem is within js That would at least solve the overload issues in his console output
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
ScuttlePeak
ScuttlePeak4w ago
Basically. But thankfully there are workarounds Try one of the solutions i put above

Did you find this page helpful?