Giter Site home page Giter Site logo

Comments (2)

trusktr avatar trusktr commented on August 28, 2024 1

Hey, before you get to it, I thought I'd include some ideas here:

Some people would love to be able to specify the types of getters differently from the setters.

For example, the following is something we can normally do in plain JavaScript:

class Vec2 {
  x: number = 0
  y: number = 0
  add(other: Vec2) {}
  sub(other: Vec2) {}
  // etc
}

class Thing {
  _pos = new Vec2

  get position(): Vec2 {
    return this._pos
  }
  set position(val: [number, number] | Vec2) {
    if (Array.isArray(val)) {
      this._pos.x = val[0]
      this._pos.y = val[1]
      return
    }
    this._pos = val
  }
}

const thing = new Thing

const pos = thing.position // pos is type Vec2

console.log(pos.x, pos.y) // 0 0

thing.position = [1, 2] // this is ok, no type error

console.log(pos.x, pos.y) // 1 2

thing.position = new Vec2 // We can also do this, no type error

Here's the (looong) TypeScript issue (with many upvotes): microsoft/TypeScript#2521

In particular, this comment has a good summary of why it is needed. Basically, there are many existing APIs, especially in the DOM, that already use these patterns in practice, but TypeScript can not type them properly.

from hegel.

JSMonk avatar JSMonk commented on August 28, 2024

Currently, getters/setters are not supported. But it will be. We will close the issue after adding the support. Thank you for your contribution ^_^.

from hegel.

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.