1

I am trying to make my log method to be generic. What I need as the main concept is as below:

class A {
    user_id: number;
    // and some other props...
}

class B {
    service_name: string;
    // and some other props...
}

function log<T>(...T parameters) {
    // logging the input parameters
}

log<A>(
    A.user_id = 123
);

log<B>(
    B.service_name = 'my_service_name';
);

What I have tried so far is as below, but I cannot make it working. I am doing this, because my project is getting bigger and more levels and entries would be added day-to-day.

enum levels {
    error,
    info,
}

enum entries {
    user_id,
    service_name,
}

function log(level: levels, ...entries: entries[]) {
    // perform the actual log...
}

log(
    levels.info,
    entries.user_id = 123
);

log(
    levels.error,
    entries.service_name = 'my_sevice_name'    
);

The reason that I have used enums in my example, is that I wanted to force the developer to choose a predefined value from a list and not to use magic numbers or magic strings.

4
  • 1
    Can you please explain in more detail what you are trying to achieve...I don't understand the use of enums in the seconds codeblock Commented May 15, 2021 at 21:16
  • I will edit my question now, to be more clear. Commented May 15, 2021 at 21:23
  • try changing the enums to interface or a plain object literal. Commented May 15, 2021 at 21:23
  • 2
    Ok so what you want to do is limit the user's choices to fixed values for the parameters Commented May 15, 2021 at 21:26

2 Answers 2

2

Is it as simple as using unions to define the keys?

type Level = "error" | "info"

type EntryKey = "user_id" | "service_name"

type Entry = Partial<{
  [K in EntryKey]:string
}>

function log(level: Level, ...entries: Entry[]) {
    // perform the actual log...
}

log("error", {service_name:"something"})
Sign up to request clarification or add additional context in comments.

1 Comment

Ah! This was more what I was talking about earlier, List of types or unions
1

If you want your second solution to "work" you'd have to change the function calls to this:

enum levels {
    error,
    info,
}

interface entry {
    user_id,
    service_name,
}

function log(level: levels, ...entries: entry[]) {
    // perform the actual log...
}

log(
    levels.info,
    { user_id: 123 }
);

log(
    levels.error,
    { service_name: 'my_sevice_name' }
);

9 Comments

I do not want the developer to freely type keys in the object. I want to provide a list for developer to choose from.
Can you please post an example in your question
Then you are not wanting to use Generics
Give them a list of inputs input | input2 | input3 etc...
I don't want to have several inputs, because the entries will be a lot and there would be a lot number of inputs then.
|

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.