Skip to content

Generics aren't working as expected when using them with Partial #43125

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
ghost opened this issue Mar 7, 2021 · 2 comments
Closed

Generics aren't working as expected when using them with Partial #43125

ghost opened this issue Mar 7, 2021 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@ghost
Copy link

ghost commented Mar 7, 2021

Bug Report

πŸ”Ž Search Terms

generic partial

πŸ•— Version & Regression Information

4.2.2

⏯ Playground Link

Playground Link

πŸ’» Code

type FunctionType<T> = {
  func(arg: Partial<T>): any;
};

type StringType = {
  str: string;
};

function example<T extends StringType>(ft: FunctionType<T>) {
  ft.func({ str: "" });
}

πŸ™ Actual behavior

The code doesn't compile because ft.func({ str: "" }) gives the following error

Argument of type '{ str: ""; }' is not assignable to parameter of type 'Partial<T>'.

πŸ™‚ Expected behavior

I would expect the code to compile

@MartinJohns
Copy link
Contributor

MartinJohns commented Mar 7, 2021

The type "abc" extends the type string.
The type { str: "abc" } extends the type { str: string }.
"" is not a valid value for the type "abc".

This is unrelated to Partial<T>. It's due to your misunderstanding of generics.

Here's a simplified example of the problem at hand:

function example<T extends string>(): T { return ""; }

example<"abc">();

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 8, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' 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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants