Code Monkey home page Code Monkey logo

Comments (4)

papb avatar papb commented on June 30, 2024 1

Also, in the future, prefer using a permalink when linking to code. So instead of

https://github.com/sindresorhus/serialize-error/blob/main/index.js#L160

You should use:

https://github.com/sindresorhus/serialize-error/blob/2a77169cae632802b75c9962b8ae9a482c978ae2/index.js#L160

To get this link:

image

from serialize-error.

papb avatar papb commented on June 30, 2024

Should the type of return value must be also function as well?

No...

If we still want to convert function value to string,

According to this test, yes we do.

The type definition for input & return value should be any or unknown since they don't share the same type.

There's nothing wrong with input type and return type not sharing the same type.

from serialize-error.

 avatar commented on June 30, 2024

There's nothing wrong with input type and return type not sharing the same type.

@papb What I wanted to say is that the current type definition is wrong

https://github.com/sindresorhus/serialize-error/blob/main/index.d.ts#L108

Type definition is enforcing that the return value should have the same type of the input, while we convert function inputs to string

from serialize-error.

fregante avatar fregante commented on June 30, 2024

Type definition is enforcing that the return value should have the same type of the input

That is correct. serializeError returns the same value if it's not an object and not a function

serialize-error/index.js

Lines 141 to 166 in 2a77169

export function serializeError(value, options = {}) {
const {
maxDepth = Number.POSITIVE_INFINITY,
useToJSON = true,
} = options;
if (typeof value === 'object' && value !== null) {
return destroyCircular({
from: value,
seen: [],
forceEnumerable: true,
maxDepth,
depth: 0,
useToJSON,
serialize: true,
});
}
// People sometimes throw things besides Error objects…
if (typeof value === 'function') {
// `JSON.stringify()` discards functions. We do too, unless a function is thrown directly.
return `[Function: ${value.name ?? 'anonymous'}]`;
}
return value;
}

The logic is simply:

  • if it's an object, return an object
  • if it's a function, return a string
  • if it's anything else, return it as is

The return is never unknown. Even if it is, we know it's Input, so we return it as such.

from serialize-error.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.