Coder Social home page Coder Social logo

Comments (1)

ahrjarrett avatar ahrjarrett commented on June 18, 2024

@sindresorhus asked me to cross post a reply I made to a comment by @ootidea on #647 here.

Looking at this issue, I'm not sure that they're directly applicable (I agree that this issue is probably a bug), but since it might lead to other issues being created, I'll go ahead and put them here.

Please don't forget to tag me if you'd like me to discuss further, otherwise I'll probably miss it in my inbox!

@ootidea's comment:
#647 (comment)

LastArrayElement<[]> should be undefined, but it is never.
LastArrayElement<T[]> should be T | undefined, but it is T.
LastArrayElement<[...1[], 2]> should be 2, but it is 1 | 2.
LastArrayElement<[1, 2?]> should be 1 | 2, but it is never.
LastArrayElement<[1?, 2?]> should be 1 | 2 | undefined, but it is never.
LastArrayElement<[0, 1, 2?, ...3[]]> should be 1 | 2 | 3, but it is 0 | 1 | 2 | 3 | undefined.
LastArrayElement should be any, but it is unknown.

@ahrjarrett's response:
#647 (comment)

For instance, the expectation that LastArrayElement<[]> return undefined is useful only as the return type of a term-level JavaScript function that gets the last element of an array, or returns undefined if the array is empty.
My 2 issues with this:
Array.prototype.last does not exist (AFAIK). You could write a function called last that behaves the way you describe -- but you could also write a function that returns an error message if the array is empty, is throws, or does any number of things instead. Choosing undefined out of any other option feels somewhat arbitrary.
This doesn't seem to be in alignment with the design goals of type-fest, since it doesn't export (AFAIK) any term-level API. If someone tried to compose LastArrayElement with another type-level function, getting undefined would be very surprising behavior, because the only way that would happen would be if the value undefined was in the array.

from type-fest.

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.