DimitriosP
DimitriosP

Reputation: 135

Typescript arrow function with signature accepts wrong parameter

Consider the type:

type GenericFunction = <T>(props: Array<T>) => void

and the arrow function:

const test: GenericFunction = <X>(props: X) => {
let dd: X }

How come that the test function accepts X as argument although the type GenericFunction defines Array<T> as parameter?

Upvotes: 0

Views: 198

Answers (1)

hendra
hendra

Reputation: 2651

There are types for X that will satisfy the signature of GenericFunction, that's why the compiler does not give an error. Theres is just an implicit constraint now that X must be an array type.

Only if you add a constraint for X that conflicts with this implicit constraint you get an error:

// this gives a compile error
const test: GenericFunction = <X extends number>(props: X) => {};

Upvotes: 1

Related Questions