#Printing error message using generic type as value

14 messages · Page 1 of 1 (latest)

solemn spire
#

I'm pretty sure it's not possible but I'm hoping I'm wrong. I want to throw an error message that says something like "expected number, received string", but for a function that uses generics. Something like this:

function makeData<T>(data: T, priority: number) {
  if (data === null || data === undefined) {
    throw new Error(`TypeError: variable "data" is invalid. Expected ${T}`);
  }
  // ...
}

I would also be happy to learn another way to type this function, such that passing null or undefined to this it will make TypeScript flag it as an error (currently it does just fine for other data types just not these two).
Thanks!

south sundial
#

I would also be happy to learn another way to type this function, such that passing null or undefined to this it will make TypeScript flag it as an error (currently it does just fine for other data types just not these two).
to do this you can use {} (which is a type that means "any value which could have properties", which excludes null and undefined) as a generic constraint:

wooden tulipBOT
#
function makeData<T extends {}>(data: T, priority: number) {
  // ...
}

makeData('foo', 0)
makeData(true, 0)
makeData(42, 0)
makeData([], 0)
makeData({ some: { fancy: ['object'] }}, 0)

makeData(null, 0)
//       ^^^^
// Argument of type 'null' is not assignable to parameter of type '{}'.
makeData(undefined, 0)
//       ^^^^^^^^^
// Argument of type 'undefined' is not assignable to parameter of type '{}'.
south sundial
#

depending on what you do in the function and what its return type is this function might not even need to be generic (function makeData(data: {}, priority: number) might work fine)

solemn spire
#

That seems easy enough, did not think about it thanks!

#

I just tried it and works perfectly. Plus, it also made me realize something's wrong with my TypeScript version or something because on my machine it wouldn't show an error but it does over on typescriptlang playground. Thanks a lot!

south sundial
#

check your tsconfig.json settings. maybe you're running in non-strict mode?

#

specifically sounds like strictNullChecks could be to blame, but i always recommend "strict": true to opt into all strict settings

solemn spire
#

Ah, good thinking but that was not it. I noticed however I had mindessly copied the tsconfig from a previous project and the include option pointed to the wrong directory. This is just for a quick prototype so I didn't pay much attention to it. Thanks again!

#

Btw I tried to mark this as resolved but the tags don't do anything (I can change the subject but not the status of the thread). Should I just close it?

south sundial
#

let me try:

#

!resolved

wooden tulipBOT
#

@solemn spire
Because your issue seemed to be resolved, this post was marked as resolved by @south sundial.
If your issue is not resolved, you can reopen this post by running !reopen.
If you have a different question, make a new post in #1057653400046674112.

south sundial
#

oh, if you meant that you tried to manually tweak the tags, that won't work. the bot manages those. you can do it via the !resolved command like i did above