0

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?

1 Answer 1

1

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) => {};
Sign up to request clarification or add additional context in comments.

1 Comment

This is understandable but it looks kind of incorrect to me. For example the test function is going to be called like test<number>(new Array<number>()). So in my mind X in this case represents number and not Array<number>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.