Code Monkey home page Code Monkey logo

Comments (7)

hpr avatar hpr commented on May 28, 2024 9

This doesn't work with a new expected error test case, because strings also have a .length property.

  // @ts-expect-error
  Length<'hello world'>,

This will pass the current specs by requiring an array:

type Length<T extends readonly any[]> = T['length']

from type-challenges-solutions.

abhijit-hota avatar abhijit-hota commented on May 28, 2024 1

@hpr While the solution is correct, I've to slightly extend the reasoning.
If it was just excluding strings, the following would also work:

type Length<T extends any[]> = T['length']

But it fails because it expects readonly types. So the solution provided by @hpr works.

from type-challenges-solutions.

matiasfha avatar matiasfha commented on May 28, 2024 1

My take was this one to avoid the strings

type Length<T> = T extends readonly any[] ? T['length'] : never 

After that I notice that the constraint can be set in the generic too

from type-challenges-solutions.

S1ngS1ng avatar S1ngS1ng commented on May 28, 2024 1

Based on the positive test cases:

Expect<Equal<Length<typeof tesla>, 4>>,
Expect<Equal<Length<typeof spaceX>, 5>>

Note the typeof tesla instead of tesla, the current solution no longer works.

And regarding the negative test cases:

// @ts-expect-error Length<5>,
// @ts-expect-error Length<'hello world'>

If I understand correctly, since 5 and 'hello world' needs to throw an error, we then need to limit T within generic, rather than in the assignment expression (to the right of =). Hence the solution should be:

type Length<T extends readonly unknown[]> = T['length'];

from type-challenges-solutions.

ghaiklor avatar ghaiklor commented on May 28, 2024

@hpr you are right. Having a constraint over an array seems like more natural even, comparing to my original solution.

from type-challenges-solutions.

AurelienWebnation avatar AurelienWebnation commented on May 28, 2024

@ghaiklor Can you update your solution with ⬇️

type Length<T extends readonly any[]> = T['length'];

This will be usefull for other users.

from type-challenges-solutions.

ghaiklor avatar ghaiklor commented on May 28, 2024

@AurelienWebnation feel free to add a PR with the proposed update. As I see it, it can be placed after the original solution with the note that there is an alternative solution.

from type-challenges-solutions.

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.