Comments (2)
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.
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)
- Do not expand types for mutually recursive functions HOT 1
- Unable to call super in subclass HOT 1
- Comment support? HOT 1
- Cannot read property 'getValueType' of undefined HOT 2
- Expose $StrictUnion and similar types as generic HOT 1
- Demo 2 is not compiling with TypeScript 4.2.3
- Relational operations should inferred for string as well HOT 2
- Allow function covariance/contravariance
- Inference from pattern matching HOT 7
- The object required is inferenced as unknown
- Cannot read properties of undefined (reading 'type') HOT 11
- Excess property checks for destructured arguments HOT 8
- Old school classes are not supported
- Project status HOT 2
- Mention another benefit of Hegel in the documentation HOT 1
- Option to add interfered types on actual code
- Maximum stack when inferring classes with a method that calls another method
- Document (automate?) maintenance of typings package
- How to contribute? HOT 3
- Types as comments syntax? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hegel.