Skip to content

No typecheck errors on optional object method param #16247

Closed

Description

TypeScript Version: 2.3.4

Code

type MethodOptions = {
    opt1?: string
    opt2?: number
}
class Test {
    method(options?: MethodOptions) {
    }
}

var test = new Test();
test.method("ciao");  //there should be an error here
test.method(1); // idem
test.method([]); // idem

directly on playground

Expected behavior:
Type checking errors

Actual behavior:
No errors

Activity

  1. ikatyang commented on Jun 4, 2017

    @ikatyang
    Contributor

    In my opinion, MethodOptions is considered to be one of these types:

    • {}
    • {opt1: string}
    • {opt2: number}
    • {opt1: string, opt2: number}

    So "ciao" | 1 | [] is a valid MethodOptions, since they match the type of {}.

  2. jwbay commented on Jun 4, 2017

    @jwbay
    Contributor

    Duplicate of #7485, fixed by #16047

  3. locked and limited conversation to collaborators on Jun 14, 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 created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions