Skip to content
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

better type indication for partial/optional type assignment #52724

Closed
5 tasks done
loynoir opened this issue Feb 11, 2023 · 2 comments
Closed
5 tasks done

better type indication for partial/optional type assignment #52724

loynoir opened this issue Feb 11, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@loynoir
Copy link

loynoir commented Feb 11, 2023

Suggestion

πŸ” Search Terms

  • type indication
  • assignment
  • Partial<T>
  • optional type key?: value

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Better type indication for assignment to Partial<T>, or said key?: value

πŸ“ƒ Motivating Example

Before feat

interface IFoobar {
  a: number
  b: number
  c: number
}

function reproduce() {
  const foobar: Partial<IFoobar> = {}

  foobar.a = 42
  foobar
  // Partial<IFoobar>

  foobar.b = 42
  foobar
  // Partial<IFoobar>

  foobar.c = 42
  foobar
  // Partial<IFoobar>
}

πŸ’» Use Cases

After feat

interface IFoobar {
  a: number
  b: number
  c: number
}

function reproduce() {
  const foobar: Partial<IFoobar> = {}

  foobar.a = 42
  foobar
  // Partial<IFoobar> & Pick<IFoobar, 'a'>

  foobar.b = 42
  foobar
  // Partial<IFoobar> & Pick<IFoobar, 'a' | 'b'>

  foobar.c = 42
  foobar
  // IFoobar
}
@jcalz
Copy link
Contributor

jcalz commented Feb 12, 2023

Essentially a duplicate of #42384; narrowing of properties doesn’t currently narrow parent objects.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 14, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants