Reputation: 4292
I'm following the docs but it seems my syntax is off from the interface examples. I'm testing this code on the playground at http://www.typescriptlang.org/play/?ssl=5&ssc=9&pln=5&pc=23#code/FDCWDsBcFMCcDMCGBjaACA8gIwFZoN7BrEFEnnjQDOMAJgAqwD2ADnJAJ4BcaNsEAczLEAvsDHBkTcDTRNcPbHgC8BNJRrQGzNrE48A5BroA1RABsArtANoJUmU3PQAdOaYCAFPJwvjWxlZ2DgBKAG4QSWlZHwAmHnx1ajpA3U543kh+cAE7NFVE-20gvQ4MgwA3C2tY23top1d3Lzi-ZICdYNjwoA
// Example 1.
// has a red lint on myProperty: "Unused label."
// also a red lint on string: "string is a type but being used as a value"
interface Obj {
{
myProperty: string
}
}
const obj: Obj = { myProperty: 'value' }
console.log(obj.myProperty); // console error: string is not defined
// Example 2: Works as expected
const obj2: { myProperty2: string } = { myProperty2: 'value2' }
console.log(obj2.myProperty2);
What is the proper syntax for the single property object defined in the interface above?
Update 1
Here is an example POST response from an API I'm working with, I'm curious what the best way to write an interface to model this would be:
HTTP/1.1 201 Created
{
"script_tag": {
"id": 870402694,
"src": "https://djavaskripped.org/fancy.js",
"event": "onload",
"created_at": "2019-10-16T16:14:18-04:00",
"updated_at": "2019-10-16T16:14:18-04:00",
"display_scope": "all"
}
}
Upvotes: 0
Views: 24
Reputation: 299
You should name the container property as well like so
interface Obj {
containerProperty: {
nestedProperty: string;
}
}
Or if you don't want nesting you can just define your interface like this
interface Obj {
myProperty: string;
}
Upvotes: 1