Skip to content

Solves Record.get() issue. Second parameter notSetValue made optional #1445

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

Closed
wants to merge 7 commits into from

Conversation

olivr70
Copy link

@olivr70 olivr70 commented Nov 16, 2017

The sample code in the doc docs not compile.

const ABRecord = imm.Record({ a: 1, b: 2 })
const myRecord = new ABRecord({ b: 3 })
let x_a = myRecord.get('a') // compile error: 2 arguments expected, only 1 received

The reason is that Record.get() is defined as

get<K extends keyof TProps,D>(key: K, notSetValue: any): TProps[K];

Argument notSetValue faces 2 issues

  • it is required (both js implementation and doc consider it to be optional : "notSetValue will be returned if provided")
  • notSetValue should not be any :
  • either is should be required to have the same type as the property (solution implemented in this PR)
  • or be typed as a generic parameter (see alternative solution below)

Alternative solution

get<K extends keyof TProps, D>(key: K, notSetValue?: D): TProps[K] | D;

Copy link

@asazernik asazernik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate of #1461; should be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants