0
interface A
  {
    x:string; y:string
  };

var a: A[] = [];
a.push(
  {x: "a", 
   y: "b"});

a.slice(-1).x = "foo";

That last line gets an error I think because the result of a.slice(-1) is not of a known type, so it says "no known property x".

  1. Is my diagnosis correct?
  2. What's the right way to do that?
2
  • Can you please post the exact error you are getting? Commented Jul 7, 2015 at 19:18
  • From the official close reasons: Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Commented Mar 25, 2018 at 10:57

1 Answer 1

0

The problem is that a.slice(-1) returns an array (the slice) even though it has only one element. An array does not have the "x" property. But each element does.

So the following does what is expected:

a.slice(-1)[0].x = "foo";
Sign up to request clarification or add additional context in comments.

2 Comments

TypeScript saving ya ;)
yeah, but it could have given a more useful error message, like: value typeof "array" does not have property "x".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.