在typescript中声明Object数组

时间:2017-11-07 18:24:46

标签: typescript

我想宣布:

type ArticleModel = {
    _id: number;
    name: string;
}

然后在我班上:

public models: ArticleModel[];

模型应该像你猜测的那样是一个ArticleModel数组。

但是node_modules / webpack / node_modules / webpack-core / lib / NormalModuleMixin.js抛出:

error TS2322: Type 'Object[]' is not assignable to type '{ _id: number; name: string; }[]'.
  Type 'Object' is not assignable to type '{ _id: number; name: string; }'.
    Property 'id' is missing in type 'Object'..

任何帮助都将不胜感激。

提前致谢。

2 个答案:

答案 0 :(得分:3)

好吧我很糟糕,我后来又重新声明了这个变量:

String userImage = null;
String postKey = null;
String uid = null;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
    mDatabaseUsers = FirebaseDatabase.getInstance().getReference("Users");
    mUsers = FirebaseDatabase.getInstance().getReference("Users");
    mDatabaseUsers.keepSynced(true);
    mAuth = FirebaseAuth.getInstance();
    mPostList = findViewById(R.id.postList);

    mPostList.setHasFixedSize(true);
    mPostList.setLayoutManager(new LinearLayoutManager(this));
}

@Override
protected void onStart() {
    super.onStart();

    mAuth.addAuthStateListener(mAuthListener);

    final FirebaseRecyclerAdapter<Post, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(
            Post.class,
            R.layout.post_row,
            PostViewHolder.class,
            mDatabase) {

        @Override
        protected void populateViewHolder(PostViewHolder viewHolder, Post model, int position) {

            postKey = getRef(position).getKey();

            viewHolder.setImage(getUserImage());
            viewHolder.setFrom(model.getFrom());
            viewHolder.setTo(model.getTo());
            viewHolder.setPrice(model.getPrice());
            viewHolder.setDate(model.getDate());
            viewHolder.setDate(model.getDate());
            viewHolder.setCar(model.getCar());

            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent particularPostIntent = new Intent(MainActivity.this, ParticularPostActivity.class);
                    particularPostIntent.putExtra("PostId", postKey);
                    startActivity(particularPostIntent);
                }
            });
        }
    };

    mPostList.setAdapter(firebaseRecyclerAdapter);
}

public static class PostViewHolder extends RecyclerView.ViewHolder {

    View mView;

    public PostViewHolder(View itemView) {
        super(itemView);

        mView = itemView;
    }

    public void setFrom(String from){

        TextView postFrom = mView.findViewById(R.id.rowFrom);
        postFrom.setText("From: " +from);
    }

    public void setTo(String to){

        TextView postTo = mView.findViewById(R.id.rowTo);
        postTo.setText("To: " + to);
    }

    public void setPrice(String price){

        TextView postPrice = mView.findViewById(R.id.rowPrice);
        postPrice.setText("Price: " + price);
    }

    public void setDate(String date){

        TextView postDate = mView.findViewById(R.id.rowDate);
        postDate.setText("Date: " + date);
    }

    public void setCar(String car){

        TextView postCar = mView.findViewById(R.id.rowCar);
        postCar.setText("Car: " + car);
    }

    public void setImage(String image){

        ImageView userImageView = mView.findViewById(R.id.rowProfileImage);

        Picasso.with(mView.getContext()).setLoggingEnabled(true);
        Picasso.with(mView.getContext()).load(image).placeholder(R.mipmap.ic_account_circle_black_48dp).into(userImageView);
    }
}

public String getUserImage(){
    mDatabase.child(postKey).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            uid = (String) dataSnapshot.child("userID").getValue();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    mUsers.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            userImage = (String) dataSnapshot.child(uid).child("Image").getValue();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    return userImage;
}

干杯

答案 1 :(得分:1)

如果要在创建类时实例化对象数组,可以执行以下操作:

public models: ArticleModel[] = [];

这是在类构造函数中进行的。