No funded issue found.
Check out the Issue Explorer
Be the OSS Funding you wish to see in the world.
Looking to fund some work? You can submit a new Funded Issue here .
Time left
Opened
Issue Type
Workers Auto Approve
Project Type
Time Commitment
Experience Level
Permissions
Accepted
Reserved For
[Request] Support object composition via structs
microsoft
TypeScript, Dockerfile
Composition over inheritance is something that has been a part of JavaScript since the dawn of time. While it’s debatable whether it is or isn’t an effective way to approach a problem, it’s still a part of the daily lives of many JavaScript developers.
This ticket isn’t here to discuss the use-cases appropriate for composition over inheritance, instead this ticket is here to plead for the introduction of a mechanism to use it within the TypeScript we know and love.
A simplified form in normal JSwould look like:
```typescript
const createA = () => ({
actionA() {
console.log(‘Hi from A’)
}
})
const createB = () => ({
actionB() {
console.log(‘Hi from B’)
}
})
const createComposite = () => ({
...createA(),
...createB(),
})
const composite = createComposite()
composite.actionA()
composite.actionB()
```
This works fine in Typescript too, however dealing with composition through classes is super cumbersome as it involves redeclaring types and proxying behaviours and properties.
```typescript
class A {
actionA() {
console.log(‘Hi from A’)
}
}
class B {
actionB() {
console.log(‘Hi from B’)
}
}
class Composite {
private a: A
private b: B
constructor(){
this.a = new A()
this.b = new B()
}
actionA() {
this.a.actionA()
}
actionB() {
this.b.actionB()
}
}
```
As you can imagine, this redeclaration becomes annoying to deal with as requirements grow.
The TypeScript community has come up with solutions such as mixins or decorators, but there is no simple and smooth solution.
While I know it's important to the TypeScript team to not take features from other languages, currently there is a hole in TypeScripts support for compositional approaches and thus I feel it's important we look to people who have seen this challenge and addressed it in a way which feels JavaScript-y and preserves the consistency of TS.
Thankfully, Go/Golang is surprisingly JavaScripty and the developers behind the project have considered and created a solution to this. Their solution was to declare a struct object type which can be given methods and composed with other struct objects.
The great thing is that the implementation is essentially 1:1 syntax sugar for factory functions.
Dig it:
interactive example:
https://play.golang.org/p/sskWaTpJgr
```go
type A struct {}
func (A) actionA() {
fmt.Println(“Hi from A”)
}
type B struct {}
func (B) actionB() {
fmt.Println(“Hi from A”)
}
type Composite struct {
A,
B,
}
```
To break it down a little
```go
// Declare an object
type A struct {}
/*
Assign a function to that object
func (targetObject) name (...params) {
...body
}
*/
func (A) actionA() {
fmt.Println(“Hi from A”)
}
```
I'm not going to suggest syntax but I do want to illustrate that the typescript implementation can be more JavaScript-y
```typescript
struct A {
actionA() {
console.log(‘Hi from A’)
}
}
struct Composite {
...A
}
const createComposite = () => Composite{}
const composite = createComposite()
```
## Checklist
My suggestion meets these guidelines:
* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
* [?] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).
Setup your profile
Tell us a little about you:
Skills
No results found for [[search]] .
Type to search skills..
Bio Required
[[totalcharacter]] / 240
Are you currently looking for work?
[[ option.string ]]
Next
Setup your profile
Our tools are based on the principles of earn (💰), learn (📖), and meet (💬).
Select the ones you are interested in. You can change it later in your settings.
I'm also an organization manager looking for a great community.
Back
Next
Save
Enable your organization profile
Gitcoin products can help grow community around your brand. Create your tribe, events, and incentivize your community with bounties. Announce new and upcoming events using townsquare. Find top-quality hackers and fund them to work with you on a grant.
These are the organizations you own. If you don't see your organization here please be sure that information is public on your GitHub profile. Gitcoin will sync this information for you.
Select the products you are interested in:
Out of the box you will receive Tribes Lite for your organization. Please provide us with a contact email:
Email
Back
Save