Skip to content

Add Defaultable<T> #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2025
Merged

Add Defaultable<T> #50

merged 2 commits into from
Mar 15, 2025

Conversation

waynemwashuma
Copy link
Collaborator

@waynemwashuma waynemwashuma commented Mar 5, 2025

Objective

Adds a new interface which enforces a class to have a static method default() which is used to create a default object of the class.

Currently cannot be directly enforced on a class as explained in this issue.Will be enforced in functions/methods which accept classes.

This was inspired by the rust language Default trait.

Solution

N/A

Showcase

Javascript:

class Something {
  static default(){
    return new Something()
  }
}

/**
  * @template T
  * @param {Defaultable<T>} that
  * @returns T
  */
function createSomething(that){
  const a = Something.default()
  return a
}

const something = createSomething(Something)

Typescript:

class Something {
  // this is the method required by `Defaultable<T>`
  static default(){
    return new Something()
  }
}

function createSomething<T>(that:Defaultable<T>):T{
  const a = Something.default()
  return a
}

const something = createSomething(Something)

Migration guide

N/A

Checklist

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.

@waynemwashuma waynemwashuma merged commit 4f966a6 into dev Mar 15, 2025
1 check passed
@waynemwashuma waynemwashuma deleted the defaultable-util branch March 15, 2025 12:20
@waynemwashuma waynemwashuma added the type:enhancement New feature or request label Mar 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant