Giter VIP home page Giter VIP logo

Comments (4)

Harris-Miller avatar Harris-Miller commented on July 18, 2024 1

@TClark1011 that's a fantastic solution for the issue with the generic. It won't solve the problem for all the functions that share this issue, but works for take and takeLast

#70

from types.

Harris-Miller avatar Harris-Miller commented on July 18, 2024

The problem you have called out is not isolated to take and takeLast. It's also true for any curried function like this where the generic is set within the return type

For whatever reason, when you have a return type that is more than a single possibility, generics on those get immediately evaluated. I have a whole write up on it here: #54

The flip side of your fix is you lose support for the overload xs: string. However, I've been leaning more and more towards removing the extra overloads as my discussion suggests

Here are other alternatives that will solve you problem in the mean time: https://tsplay.dev/m0Voxw

from types.

TClark1011 avatar TClark1011 commented on July 18, 2024

My solution can still accommodate the string overload, we just have to add another signature for it, so the new declaration would look like this:

export function take<T>(n: number, xs: readonly T[]): T[];
export function take<T>(n:number): (xs: readonly T[]) => T[];
export function take(n: number, xs: string): string;
export function take(n:number): (xs: string) => string;

from types.

TClark1011 avatar TClark1011 commented on July 18, 2024

Okay I actually figured out a workaround that can handle both the string and array curried signature and allow the generic to be evaluated after the original call: https://tsplay.dev/N7QPGW

I understand this may not be the ideal solution, it was just an idea I had and thought it was worth mentioning.

from types.

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.