TS type inference error

I’m not sure if it’s a bug or an intentional one.

lines.ts:

let lines: THREE.LineSegments<THREE.BufferGeometry, THREE.LineBasicMaterial>;
const lineGeometry = new THREE.BufferGeometry();
const lineMaterial = new THREE.LineBasicMaterial();
// ok!
lines = new THREE.LineSegments(lineGeometry, lineMaterial);
// no!
// Error: Line<Geometry | BufferGeometry, Material | Material[]>
// Attribute 'attributes' does not exist on type' geometry '
console.log(lines.geometry.attributes);

I know “LineSegments” inherit from “Line”, so I look at LineSegments.d.ts and Line.d.ts files.

Caused by LineSegments.d.ts not declaring their own ‘. geometry’ and ‘. material’.
LineSegments.d.ts

BTW: I don’t want to use as to assert, which is ugly to me.:rofl:

  1. You’re declaring let lines and const lines. You can’t redeclare two variables of the same name.
  2. When assigning a type to lines, it should be let lines: THREE.LineSegments; There’s no need to specify what parameters will go into its constructor within < >.
let lines: THREE.LineSegments;

const lineGeometry = new THREE.BufferGeometry();
const lineMaterial = new THREE.LineBasicMaterial(); 
lines = new THREE.LineSegments(lineGeometry, lineMaterial);
1 Like

Yes, there is a problem here. This is my error in writing the example. Please ignore first, because this problem is extracted from a large file.At the same time, I have modified the example.

Maybe you didn’t understand my problem. The TS inference error occurred in this paragraph:

Oh, in that case I’m sorry to say you’re going to have to use type-asseertion because lines.geometry could be either a BufferGeometry or Geometry, so you have to tell the compiler which one you’re expecting.

Perhaps instead of using as, you could use:

const geom: BufferGeometry = lines.geometry;
console.log(geom.attributes);

or since you’ve already specified lineGeometry's type, maybe you could use lineGeometry.attributes, although that might not be possible if it’s in a different method of the class.