Skip to content

Generic type check seems cannot infer type #23763

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
Delagen opened this issue Apr 28, 2018 · 3 comments
Closed

Generic type check seems cannot infer type #23763

Delagen opened this issue Apr 28, 2018 · 3 comments
Labels
Duplicate An existing issue was already created Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@Delagen
Copy link

Delagen commented Apr 28, 2018

TypeScript Version: 2.7.0-dev.201xxxxx

Search Terms:

Code

const property: keyof IArrayItem = "test";

interface IArrayItem {
    test: string;
}

function A<T extends IArrayItem = IArrayItem>(array: T[]) {
    const t: Partial<T> = { test: "aaa" }; // Error: Type '{ test: "aaa"; }' is not assignable to type 'Partial<T>'.
    const t2: Partial<IArrayItem> = { test: "aaa" }; // Works
}

const array: IArrayItem[] = [];
A(array);

Expected behavior:
No type check errors

Actual behavior:
Error: Type '{ test: "aaa"; }' is not assignable to type 'Partial'

Playground Link: playground

Related Issues:

@Delagen
Copy link
Author

Delagen commented Apr 28, 2018

See also this discussion DefinitelyTyped/DefinitelyTyped@64104e9

Delagen referenced this issue in DefinitelyTyped/DefinitelyTyped Apr 28, 2018
* lodash: _.get should with numeric keys, too.
Also added some better tests.

* lodash: add one more NumericDictionary overload for _.get

* lodash: more reasonable index for _.get tests

* lodash: object type should not be here (fixes #23293)

* lodash: reduce should always return T if no accumulator (#14758)

* lodash: fix iterator types, remove thisArg from documentation

* lodash: return more specific type for stubTrue and stubFalse

* lowdb: fix failing tests
@jack-williams
Copy link
Collaborator

I think this should be an error. There is no guarantee that the object literal is assignable for all possible Partial<T>. A caller is free to instantiate the generic in A to be the following type T.

type T = { test: "foo" };
type Extends<T extends IArrayItem> = T // Prove T extends IArrayItem
const example: Partial<Extends<T>> = { test: "aaa" }; // Error

@ahejlsberg
Copy link
Member

This is working as intended and is a duplicate of many existing issues, e.g. #19388, #17071, #13442.

@ahejlsberg ahejlsberg added Duplicate An existing issue was already created Working as Intended The behavior described is the intended behavior; this is not a bug labels Apr 28, 2018
@mhegazy mhegazy closed this as completed Apr 28, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants