0

If I have a class and method like below, I would like to know if there is a way I can easily declare an extra property on the f1 method?

export class Something {
    f1(): string { ... }
}

When not in a class context I can do it this way:

function f1(): string {
    // Simple example. 
    if ( ... ) {
        f1.found = true;
    } else {
        f1.found = false;
    }
    
}

namespace f1 {
    export let found = false;
}

And this will not present with an error because of the namespace merging with the function definition.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    Can you provide more information about the use case? Since methods are shared by all instances, any flag on a method would also be shared. Is that what you want? Why do you need an additional property on the method itself vs just using class property? – Felix Kling Nov 12 '21 at 08:38
  • @FelixKling Yes, so I thought I would like to try and store extra information as I would not like to change the return value and type of the function. And that this would enable me to store something like `found` or `lastKey` on the function to use like so: `s1 = new Something()` `s1.f1(...);` `if (s1.f1.found) { ... }` This is because the function will for example always return something. And also for example I can store a `lastKey` variable maybe, and get that from the function. – BlindChicken Nov 12 '21 at 08:56
  • 1
    *"as I would not like to change the return value and type of the function"* Why are you converting it to a class method then? I mean, what you want is not impossible, but if you are refactoring your code anyway, why not take the time and properly design the API? – Felix Kling Nov 12 '21 at 09:30
  • @BlindChicken Use `s1.found` instead. Or `s1.f1Found`. Or even better, just put the flag on the return value of `s1.f1()`. Like Felix said, do not put a global flag on the function object. – Bergi Nov 12 '21 at 10:48

1 Answers1

0

Please consider these examples.

First one - declare f1 as an arrow function inside constructor.

type F1 = {
  bound: boolean;
  (): string;
};

class Something {
  public f1: F1
  constructor() {

    const f1: F1 = () => 'hello'
    f1.bound = true;

    
  }

const result = new Something().f1
result.bound // boolean

Second one - declare f1 as a function outside of constructor

type F1 = {
  bound: boolean;
  (): string;
};

function f1() {
  return ''
}

f1.bound = true;

class Something {
  public f1: F1
  constructor() {
    this.f1 = f1.bind(this)
  }
}

const result = new Something().f1
result.bound // boolean

Third

type F1 = {
    bound: boolean;
    (): string;
};

class Something {
    f1: F1
    constructor() {
        function f1() {
            return 'String'
        }
        f1.bound = true
        this.f1 = f1.bind(this)
    }
}

const result = new Something().f1
result.bound // boolean

It also depends if you use this in f1 method.

Please see this question regarding using static properties in a function in typescript.

  • A couple of notes: (1) `f1.bind(this)` is unnecessary in the first example because you declared `f1` as an arrow function. (2) `result.bound` won't exist in either example because the new function returned by `.bind` doesn't retain the properties of the original function. (3) Assuming that you set `bound` on the arrow function in example 1 or the bound found in example 2, then that flag will be unique to each instance of the class. This may or may not be what the OP wants (I asked but their response didn't address that). – Felix Kling Nov 12 '21 at 09:27
  • @FelixKling thanks, you are right, I removed `bind`. As for the `(3)` I'm not sure either why OP wants to use this flag, just provided him with some options – captain-yossarian from Ukraine Nov 12 '21 at 09:44
  • Re (3), I actually wanted to say that it would worth to call out that behavior, just to make sure. Your second example would still need to be changed to `this.f1.bound = true;`. – Felix Kling Nov 12 '21 at 10:16
  • @FelixKling thanks. for the feedback, I have provided third example – captain-yossarian from Ukraine Nov 12 '21 at 10:25