Typescript: Default values for parameter of a constrained generic type

Created on 27 Nov 2018  路  4Comments  路  Source: microsoft/TypeScript

TypeScript Version: 3.1.4

Search Terms:

Code

function f<T extends boolean>(some: T = false) {}

Expected behavior:

TS would happily accept and type-check this code (leaving me solving type narrowing with guards later on)

Actual behavior:

Type 'true' is not assignable to type 'T'.

Playground Link: here is a link

Related Issues:

Most helpful comment

@DeTeam Yes you're right that it's not exactly the same, sorry about that. I think the reasoning will still be the same. You can get into situations like this:

f<true>();

I think this is a duplicate, or at least related: #28154

All 4 comments

Duplicate of #21521

Nice catch @jack-williams, thanks!

It's not exactly a duplicate. I'm providing a constraint for T and expecting no need to do as T for the default value.

@DeTeam Yes you're right that it's not exactly the same, sorry about that. I think the reasoning will still be the same. You can get into situations like this:

f<true>();

I think this is a duplicate, or at least related: #28154

Yup, that one seem related.

We can close this one and continue in the original thread then :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbondc picture jbondc  路  3Comments

seanzer picture seanzer  路  3Comments

dlaberge picture dlaberge  路  3Comments

wmaurer picture wmaurer  路  3Comments

weswigham picture weswigham  路  3Comments