Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array Type Interface dotted notation #4345

Closed
S-ed opened this issue Aug 17, 2015 · 1 comment
Closed

Array Type Interface dotted notation #4345

S-ed opened this issue Aug 17, 2015 · 1 comment
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Question An issue which isn't directly actionable in code

Comments

@S-ed
Copy link

S-ed commented Aug 17, 2015

interface Interface {
      [id: string]: string;
}

var a:Interface = {
      b: 'foo'
};

a.b = 'bar';

Throws an error:

Property 'b' does not exist on type 'Interface'

Possible to solve? Maybe treat Array index as 'any':

var a:any = {};
a.b= "foobar";

This one is ok.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Aug 17, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2015

to access the member b you need to define it in the interface. e.g:

interface Interface {
      b: string;
}

var a:Interface;
a.b = 'bar'; // OK

alternatively you can use string indexing to access your property:

interface Interface {
      [id: string]: string;
}

var a:Interface;

a["b"] = 'bar'; // OK, type of a["b"] is defined in the string indexer definition in Interface

if you give a value the type any, it can have any number of properties, and the compiler will not enforce any rules there.

@DanielRosenwasser DanielRosenwasser added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Aug 17, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants