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

The field of a generic is omitted for the function parameter but should be addible #42959

Open
5 tasks done
chrissisura opened this issue Feb 25, 2021 · 3 comments
Open
5 tasks done
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@chrissisura
Copy link

chrissisura commented Feb 25, 2021

Suggestion

πŸ” Search Terms

Generics
Narrow Extension

βœ… 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 feature would agree with the rest of TypeScript's Design Goals.

  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)

⭐ Suggestion

It would be really nice if you can extend an object with one/selection of keys, in which the types of that keys don't get extended.
The problem with this extension is, that it doesn't allow for a pipe, where you omit some part of it, as it gets added in that pipe with the correct type (e.g. string), but as the generic could be an extension of string you can no longer put it back together.
Maybe there is a workaround for this, but I couldn't find anything online

πŸ“ƒ Motivating Example

export class TemplateFormatter<TemplateInput> {
  bar(templateInput: TemplateInput): void {
    console.log(templateInput)
  }
}

const foo = <
  T extends NARRORTYPE {a: string}
>(
  b: TemplateFormatter<T>
) => (
  payload: Omit<T, 'a'>
): void => {
  const a = 'someString'
  b.bar({ ...payload, a })
}

πŸ’» Use Cases

This would allow for some really nice currying and pipes, in which objects can be put together nicely with good levels of abstractions.

@chrissisura chrissisura changed the title The field a of generic is omitted for the function parameter but should be present in the call of b.foobar The field of a generic is omitted for the function parameter but should be added later Feb 25, 2021
@chrissisura chrissisura changed the title The field of a generic is omitted for the function parameter but should be added later The field of a generic is omitted for the function parameter but should be addible Feb 25, 2021
@RyanCavanaugh
Copy link
Member

Some examples of legal and nonlegal calls to foo would provide a ton of context. What is the intended interpretation of NARRORTYPE ?

@chrissisura
Copy link
Author

Intended interpretation of NARROWTYPE

T should have a field a with the type string. The way typescript extends this field a it can have any extension of string, but there should also be a way to match exactly that type.

The reason I want to have a type variable is so that I can use a specific version of my TemplateFormatter, so that I can infer the type of the payload variable.

Examples for foo calls

legal:

const foobar = foo(new TemplateFormatter<{a: string, b: number}>())
const b = 5
foobar(b)

nonlegal:

foo(new TemplateFormatter<{b: number}>())
foo(new TemplateFormatter<{a: number, b: string}>())
foo(new TemplateFormatter<{a: Extends<string>, b: number}>())

@RyanCavanaugh
Copy link
Member

const foobar = foo(new TemplateFormatter<{a: string, b: number}>())
const b = 5
foobar(b)

Why would this be legal? Do you mean foobar({ b }) ? What is Extends<string> ?

It seems like this does what you want:

const foo2 = <T,>(b: TemplateFormatter<T & { a: string }>) => (payload: T): void => {
  const a = 'someString'
  b.bar({ ...payload, a })
}

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

2 participants