Skip to content

"Static method" or properties on functions gives rise to issue #22408

Closed
@babakness

Description

@babakness

I looked for duplicates, perhaps this is related?
#5863

Not sure...

Suppose we have some IO function that returns an object. In Javascript, Classes are more or less just functions

const IO = f => ({
  f,
  map: g => IO( (...args) => g ( f(...args) ) ) ,
  join: () => f()
})

IO.of = (val) => IO(()=>val)

The of class function raises an issue with Property 'of' does not exist on type '(f: any) => { f: any; map: (g: any) => any; join: () => any; }'.

Expected behavior is that TS understands that of is a property or "static method" of sorts on the IO function.

The error goes away by doing one of the following

const IO = (f => ({
  f,
  map: g => IO( (...args) => g ( f(...args) ) ) ,
  join: () => f()
})) as any

Or something like this

interface P {
  f: any,
  map: (f) => P,
  join: any
}
interface F { 
  (f): P; 
  of: (f) => P; 
}


const IO = (f => ({
  f,
  map: g => IO( (...args) => g ( f(...args) ) ) ,
  join: () => f()
})) as F


IO.of = (val) => IO(()=>val)

It seems that of should be inferred without this however. Also, in the later example, Intellisense doesn't seem to realize that map return a new copy of P interface but that is probably news for a different team ;-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions