How to Modify Interface Index

How to create an index signature for an interface

In order to use the object[key] syntax, you must inform the typescript compiler that your object can index using a specific type (for instance, using string or numbers).

From the typescript documentation:

Similarly to how we can use interfaces to describe function types, we can also describe types that we can “index into” like a[10], or ageMap["daniel"]. Indexable types have an index signature that describes the types we can use to index into the object, along with the corresponding return types when indexing.

A code example (from the documentation as well):

interface StringArray {
[index: number]: string;
}

let myArray: StringArray;
myArray = ["Bob", "Fred"];

let myStr: string = myArray[0];

So, in order to make your credentials object indexable, you should declare it like that:

export interface ICredentials {
email: string,
password?: string,
username?: string,
password_confirmation?: string,
token?: string
[key: string]: string;
}

Finding the interface index in C#

I think you want

myInterface.GetIPProperties().GetIPv4Properties().Index;

As in

var interfaces = NetworkInterface.GetAllNetworkInterfaces(); 

foreach(var @interface in interfaces)
{
var ipv4Properties = @interface.GetIPProperties().GetIPv4Properties();

if (ipv4Properties != null)
Console.WriteLine(ipv4Properties.Index);
}

Note that GetIPv4Properties() can return null if no IPv4 information is available for the interface.

This msdn page shows an example that might be helpful.

Typescript interface - index signature inconsistency

This is expected behavior.

To use the index signature you need to use the index syntax -- or use the literal assignment which you demonstrate works.

let banana: Fruit;
banana.colour = 'yellow';
banana['haveToPeel'] = true;


Related Topics



Leave a reply



Submit