Skip to content

Generic type check seems cannot infer type #23763

Closed
@Delagen

Description

@Delagen

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:

Activity

Delagen

Delagen commented on Apr 28, 2018

@Delagen
Author
added a commit that references this issue on Apr 28, 2018
jack-williams

jack-williams commented on Apr 28, 2018

@jack-williams
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

ahejlsberg commented on Apr 28, 2018

@ahejlsberg
Member

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

added
DuplicateAn existing issue was already created
Working as IntendedThe behavior described is the intended behavior; this is not a bug
on Apr 28, 2018
locked and limited conversation to collaborators on Jul 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdWorking as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Delagen@ahejlsberg@jack-williams@mhegazy

        Issue actions

          Generic type check seems cannot infer type · Issue #23763 · microsoft/TypeScript