Hegel: Type "(string) => string" is incompatible with type "<T>(T) => T"

Created on 6 May 2020  路  8Comments  路  Source: JSMonk/hegel

I'm not sure i doing it correctly but here it is

type Fn = <T>(value: T) => T

let fn: Fn = (s:string) => '2'

try

bug

All 8 comments

it's ok. <T>(T) => T is more common type and can receive any value.

how to fix error then?

not sure what are you trying to achieve but in this case a better way to describe Fn would be

type Fn<T> = (value: T) => T

let fn: Fn<string> = (s:string) => '2'

i want like so

type Fn = <T>(value: T) => T

function test(fn: Fn) {...}

test((s: string) => s)
test((s: number) => s)

type Fn<T> = (value: T) => T

function test<T>(fn: Fn<T>) {}

test((s: string) => s)
test((s: number) => s)

ok, thanks. Can it recognize automatically without generic here function test<T>(fn: Fn<T>)?

this is a question for @JSMonk

@Merciful12 is't incorrect case. Without generic in function test you can call function with any parameter:

type Fn = <T>(value: T) => T

function test(fn: Fn) {
  fn(true)
}

test((s: string) => s)
test((s: number) => s)
Was this page helpful?
0 / 5 - 0 ratings