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
Calculating Memory of a Process Using Proc File System
Synchronize Linux System Clock to Windows Ntp Service
Why Can't I Access This Folder
How to Install Flash Builder 4 or a Flash Builder Plugin on Eclipse in Ubuntu 10.04
Copying Files from Multiple Directories into a Single Destination Directory
Merge Two Files Using Awk in Linux
Adding Timestamps to Packet Payload with Tcpreplay
Output File Not Created When Reading Sequences
Linux C Socket: Blocked on Recv Call
Ld_Library_Path Failing While Trying to Run Qt App
Gitlab Ssh Requests Password and Ignoring Ssh Keys
What Is the Use Case for Epollet
How to Update a Varible from One Shell to Another Shell
Bash - How to Match Files Names to Use in Loop