Code Monkey home page Code Monkey logo

Comments (6)

RyanCavanaugh avatar RyanCavanaugh commented on May 1, 2024 1

If there's some super straightforward fix here we can take it, but I wouldn't consider this a bug.

from typescript.

jcalz avatar jcalz commented on May 1, 2024

It's not a crash. (I guess the template should explain what this means since so many people seem to choose this option erroneously.)

I'm not sure why this is supposed to be a bug; circularity warnings can be annoying but that doesn't rise to the level of a bug in the language. Yes, you can avoid warnings by changing your code. Note that the error message mentions that the problem is partially due to the lack of a type annotation on newMethods. So if I wanted to get your code working, I'd annotate like

const newMethods: {
    add<T>(this: T[], ...values: T[]): T;
} = {
    add(...values) { this.push(...values); return values[0]; }
};

Playground link

from typescript.

fatcerberus avatar fatcerberus commented on May 1, 2024

I'm honestly confused why the code is written this way (interface Array<T> extends ...) instead of just augmenting Array directly. It's really weird to do declaration merging via extends, imo.

from typescript.

Gershy avatar Gershy commented on May 1, 2024

It's not a crash. (I guess the template should explain what this means since so many people seem to choose this option erroneously.)

I'm not sure why this is supposed to be a bug; circularity warnings can be annoying but that doesn't rise to the level of a bug in the language. Yes, you can avoid warnings by changing your code. Note that the error message mentions that the problem is partially due to the lack of a type annotation on newMethods. So if I wanted to get your code working, I'd annotate like

const newMethods: {
    add<T>(this: T[], ...values: T[]): T;
} = {
    add(...values) { this.push(...values); return values[0]; }
};

Playground link

I created an issue as I feel the two examples should compile the same way, whether it's successful or unsuccessful. I can't see any reason why circular typing would invalidate one example but not the other - that is, I don't see why the spread operator would have any connection to circular type definitions.

Explicitly declaring the typing of newMethods introduces repetition and reduces maintainability - and everything works, without explicit typing, in the other example - why shouldn't it be able to work here? It feels like an inconsistency in typescript, and hence a bug (as opposed to feature request).

from typescript.

jcalz avatar jcalz commented on May 1, 2024

#49263 (comment)

from typescript.

Gershy avatar Gershy commented on May 1, 2024

I'm honestly confused why the code is written this way (interface Array<T> extends ...) instead of just augmenting Array directly. It's really weird to do declaration merging via extends, imo.

I think this way could allow for more maintainability if extending a prototype with many properties; extends automatically applies them without needing to manually enumerate the property names

from typescript.

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.