0

Here's what I'm trying (not working):

interface I<T = any> {
    value: T
}

// Doesn't work, "Cannot find name 'T'."
const foo = (i: I<T>): T => {
    return i.value;
}

I've tried various places to stick <T> in and around the method definition to try to get a new type variable name, but with no luck. Here's what does work, using function syntax:

interface I<T = any> {
    value: T
}

function foo<T> (i: I<T>): T {
    return i.value;
}

Is there an equivalent syntax for inline/fat-arrow functions?

Andrew B.
  • 1,225
  • 1
  • 13
  • 18
  • 4
    `const foo = (i: I): T => i.value;` – jcalz Jun 05 '19 at 13:57
  • 1
    btw, note [IntelliSense will tell you this if you want](https://typescript-play.js.org/#code/JYOwLgpgTgZghgYwgAgJIB4AqyC8y4gCeAfMgN4BQy1yAbnADYCuEAXMphQL4UUxMgEYYAHsQyGCJFZSACmDsMmYgEp22SjWRQIYJlHHAAdPWYQA3N15hCABxQAxKbmQ37ImBKnnkAel-IAKoAziio4BAMDMAAyhAgochiyE4iFP6udo7OeDLyijIquKSYQA) – jcalz Jun 05 '19 at 13:59
  • Thanks! Nice idea, jcalz. Unfortunately, this syntax produces an error in a .tsx file, which is part of why I wasn't able to find it. – Andrew B. Jun 05 '19 at 14:41
  • Interesting... I guess you could do `const foo: (i: I) => T = i => i.value;` – jcalz Jun 05 '19 at 15:08
  • https://github.com/microsoft/TypeScript/issues/15713 – jcalz Jun 05 '19 at 15:10

0 Answers0