Skip to content

default value of generic argument is not assignable to the extended type #28154

Closed
@barak007

Description

TypeScript Version: 3.2.0-dev.20181026

Search Terms:
default argument
default parameters
default value
default generic value
not assignable to
Code

type Z = "a" | "b"

// works fine
const x: Z = 'a'

function y<T extends Z>(a: T) { }

// works fine
y('a')  

// When you add default value the autocomplete is working fine, suggesting 'a' and 'b' but type error is present.
// Error: Type '"a"' is not assignable to type 'T'.
function y1<T extends Z>(a: T = 'a') { }

// This also working
function y2(a: "a" | "b" = 'a') { }

Expected behavior:
'a' should be assignable to T
Actual behavior:
Type Error

Playground Link:
http://www.typescriptlang.org/play/#src=type%20Z%20%3D%20%22a%22%20%7C%20%22b%22%0D%0A%0D%0Aconst%20x%3A%20Z%20%3D%20'a'%0D%0A%0D%0Afunction%20y%3CT%20extends%20Z%3E(a%3A%20T)%20%7B%20%7D%0D%0A%0D%0Ay('a')%20%20%0D%0A%0D%0Afunction%20y1%3CT%20extends%20Z%3E(a%3A%20T%20%3D%20'a')%20%7B%20%7D%0D%0A

Related Issues:
Nope.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions