可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have created this array of objects. Can someone tell me how I could define these in typescript? Is it possible to do inline or would I need two definitions?
userTestStatus xxxx = { "0": { "id": 0, "name": "Available" }, "1": { "id": 1, "name": "Ready" }, "2": { "id": 2, "name": "Started" } };
I'm looking to replace the "xxx" with a definition ( I think that's the correct word ). So that later on Typescript would alert me if I used something like userTestStatus[3].nammme by mistake.
回答1:
You are better off using a native array instead of an object literal with number-like properties, so that numbering (as well as numerous other array functions) are taken care of off-the-shelf.
What you are looking for here is an inline interface definition for your array that defines every element in that array, whether initially present or introduced later:
let userTestStatus: { id: number, name: string }[] = [ { "id": 0, "name": "Available" }, { "id": 1, "name": "Ready" }, { "id": 2, "name": "Started" } ]; userTestStatus[34978].nammme; // Error: Property 'nammme' does not exist on type [...]
If you are initializing your array with values right away, the explicit type definition is not a necessity; TypeScript can automatically infer most element types from the initial assignment:
let userTestStatus = [ { "id": 0, "name": "Available" }, ... ]; userTestStatus[34978].nammme; // Error: Property 'nammme' does not exist on type [...]
回答2:
What you have above is an object, not an array.
To make an array use [
& ]
to surround your objects.
userTestStatus = [ { "id": 0, "name": "Available" }, { "id": 1, "name": "Ready" }, { "id": 2, "name": "Started" } ];
Aside from that TypeScript is a superset of JavaScript so whatever is valid JavaScript will be valid TypeScript so no other changes are needed.
Feedback clarification from OP... in need of a definition for the model posted
You can use the types defined here to represent your object model:
type MyType = { id: number; name: string; } type MyGroupType = { [key:string]: MyType; } var obj: MyGroupType = { "0": { "id": 0, "name": "Available" }, "1": { "id": 1, "name": "Ready" }, "2": { "id": 2, "name": "Started" } }; // or if you make it an array var arr: MyType[] = [ { "id": 0, "name": "Available" }, { "id": 1, "name": "Ready" }, { "id": 2, "name": "Started" } ];
回答3:
What you really want may simply be an enumeration
If you're looking for something that behaves like an enumeration (because I see you are defining an object and attaching a sequential ID 0, 1, 2 and contains a name field that you don't want to misspell (e.g. name vs naaame), you're better off defining an enumeration because the sequential ID is taken care of automatically, and provides type verification for you out of the box.
enum TestStatus { Available, // 0 Ready, // 1 Started, // 2 } class Test { status: TestStatus } var test = new Test(); test.status = TestStatus.Available; // type and spelling is checked for you, // and the sequence ID is automatic
The values above will be automatically mapped, e.g. "0" for "Available", and you can access them using TestStatus.Available
. And Typescript will enforce the type when you pass those around.
If you insist on defining a new type as an array of your custom type
You wanted an array of objects, (not exactly an object with keys "0", "1" and "2"), so let's define the type of the object, first, then a type of a containing array.
class TestStatus { id: number name: string constructor(id, name){ this.id = id; this.name = name; } } type Statuses = Array<TestStatus>; var statuses: Statuses = [ new TestStatus(0, "Available"), new TestStatus(1, "Ready"), new TestStatus(2, "Started") ]
回答4:
Some tslint
rules are disabling use of [], example message: Array type using 'T[]' is forbidden for non-simple types. Use 'Array<T>' instead.
Then you would write it like:
var userTestStatus: Array<{ id: number, name: string }> = Array( { "id": 0, "name": "Available" }, { "id": 1, "name": "Ready" }, { "id": 2, "name": "Started" } );