Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: Explanation parameter type on class HttpParams [param: string]: string | string[];

I'm using HttpParams that is the following constructor:

constructor(options?: {
        fromString?: string | undefined;
        fromObject?: {
            [param: string]: string | string[];
        } | undefined;
        encoder?: HttpParameterCodec | undefined;
});

Can anyone explane me the meaning fromObject parameter and how to use it?

romObject?: {
     [param: string]: string | string[];
} | undefined;
like image 528
Ricardo Rocha Avatar asked Feb 06 '26 04:02

Ricardo Rocha


1 Answers

It's an optional parameter in the options argument of the constructor. It means options (that's passed in the constructor) can have a fromObject property (it's not mandatory). If it's present, it must be a map where the key is a string and the value is either string or a string array (string|string[]), or it can also be undefined.

So the following are valid

const params = new HttpParams({fromObject: {bla: 'test'}});
const params = new HttpParams({fromObject: {bla: ['test1', 'test2']}});
const params = new HttpParams({fromObject: undefined});
const params = new HttpParams({});

this is not valid:

const params = new HttpParams({fromObject: 'this will fail'});
like image 139
Andrei Tătar Avatar answered Feb 07 '26 20:02

Andrei Tătar