Skip to content

Cannot create a Partial<Generic> type in a one-liner #33134

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
gewoonwoutje opened this issue Aug 29, 2019 · 2 comments
Closed

Cannot create a Partial<Generic> type in a one-liner #33134

gewoonwoutje opened this issue Aug 29, 2019 · 2 comments

Comments

@gewoonwoutje
Copy link

gewoonwoutje commented Aug 29, 2019

TypeScript Version: 3.4.5; 3.7.0-dev.20190829

Search Terms:

Partial, Generic, inconsistent,

Code

interface MyMap {
	field: string;
}

function someGenericFunction<T extends MyMap>(input: T) {
	let y: Partial<T>;

	// falsely not allowed: 
	// ts(2322): Type '{ field: ''; }'is not assignable to type 'Partial<T>'. 
	y = { field: '', };

	// similar, but allowed
	y = {};
	y.field = '';

	// correctly not allowed
	y.anything = ''; // anything is not defined in MyMap type
	y = {
		field: '',
		anything: 123, // anything is not defined in MyMap type
	}
}

Expected behavior:
Should be able to create the generic in a one-liner .

Actual behavior:

Can be done in two lines, not in one line.

Playground Link:

http://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgLIE9VwA7IN4CwAUAJAzAQA2AJgFzIDOYUoA5gNzEC+xxMAriARhgAexCNRAWwgBxCCGjAEAMUHCxIADwAVZBAAekENQZpMOAHwAKUNn5h6OgJT5iJShDDJ09AApwUCJwlLqWnETuAPRRyPCUDFToyCCi3iGUogDuEHTI0bFgDNYATADMJSXOTujYKADkeHEUNPT19ezIXPXAZqnpDAzArCBwAEaeyGCiU7UNAUHAIWH1AHT5pMkAvPjNVHntADRdEQWMwFL8lIHHYw7IGdm57tv4XBEk6Kvk+8g77adIqQYsgEKIoFAIMJKMl+g9KJkctQXqs4CB0GAABZsP7IAHIEE-Gh4tEY7EgVj1ZDUUQQPppfQGXreUCzOrmLDYF64wikMgtA71Q7uEikrFsegARnKx0JApJ6PFFKpNLpKQZhmZyFZYDmHJw7h4RCNQA

Related Issues:
#28470

@jcalz
Copy link
Contributor

jcalz commented Aug 29, 2019

Duplicate of #13442

Note that T may be resolved with a type like

interface SomeoneElsesMap extends MyMap {
  field: "It's *my* map now";
}

So you can't safely assign {field: ""} to Partial<T>. For that matter, you can't safely do the assignment y.field = "", but for whatever reason that's allowed by the compiler.

@gewoonwoutje
Copy link
Author

Okay, I see I did not do enough search in the issue history. This issue has a lot of history being closed as duplicate. In fact, item #23015 seems to be exactly the same.

It's still a bug though, as it does not work like expected. Or it should accept both cases, or neither. But I understand that it would be a very breaking change in both cases.

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

No branches or pull requests

2 participants