microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

Home Page:https://www.typescriptlang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

at the class getter property,why is need it initialize, if not then report error code is ts(2739)

nczsl opened this issue Β· comments

commented

πŸ”Ž Search Terms

ts(2739)

πŸ•— Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

code -v :
1.89.1
dc96b837cf6bb4af9cd736aa3af08cf8279f7685
x64

tsc -v :
Version 5.4.5

⏯ Playground Link

No response

πŸ’» Code

  class CanvasInfo {
    // canvas:HTMLCanvasElement;
    context: GPUCanvasContext;
    format: GPUTextureFormat;
    renderDescriptor: GPURenderPassDescriptor;
    constructor(){}
    get width(): number {
      return this.context.canvas.width;
    }
    get height(): number {
      return this.context.canvas.height;
    }
  }
//------------------------------------------
 let cinfo: /**here display the rad under line ,report error code is ts(2739)*/CanvasInfo = {  
        context,
        format,
        renderDescriptor: {
          colorAttachments: [{
            view: context.getCurrentTexture().createView(),
            loadOp: 'clear',
            storeOp: 'store',
            clearValue: color ? { r: color.x, g: color.y, b: color.z, a: color.w } : { r: 0.30, g: 0.0, b: 0.0, a: 1.0 }
          }],
          depthStencilAttachment: {
            view: depthTexture.createView(),
            depthLoadOp: 'clear',
            depthStoreOp: 'store',
            depthClearValue: 1.0,
          }
        },
        // width: canvas.width,
        // height: canvas.height
      };

πŸ™ Actual behavior

the width/height in CanvasInfo class is a getter ,why need initialize?

πŸ™‚ Expected behavior

getter property is need provide,

Additional information about the issue

at the class getter property,why is need it initialize, if not then report error code is ts(2739)

This is working as intended.

You're assigning an object literal to your variable, and the object literal is not an instance of the class and thus won't have the getters.

commented
      let cinfo: CanvasInfo = new CanvasInfo();
      cinfo.context = context;
      cinfo.format = format;
      cinfo.renderDescriptor = {
        colorAttachments: [{
          view: context.getCurrentTexture().createView(),
          loadOp: 'clear',
          storeOp: 'store',
          clearValue: color ? { r: color.x, g: color.y, b: color.z, a: color.w } : { r: 0.30, g: 0.0, b: 0.0, a: 1.0 }
        }],
        depthStencilAttachment: {
          view: depthTexture.createView(),
          depthLoadOp: 'clear',
          depthStoreOp: 'store',
          depthClearValue: 1.0,
        }
      }; 

I modified it to be correct, it meets expectations.