Skip to main content
Known Participant
May 13, 2023
Question

TypeScript throws error when trying to use setValue on text layer

  • May 13, 2023
  • 2 replies
  • 1434 views
 

 

var newTextLayer: TextLayer = app.project.activeItem.layers.addText("Hello, world.");
newTextLayer.property("Position").setValue([0,0]);

 

 
TypeScript throws the following error:
Property 'setValue' does not exist on type '_PropertyClasses'.
Property 'setValue' does not exist on type 'PropertyGroup'.
 
Is there a better way to set properties on text layers?
This topic has been closed for replies.

2 replies

Known Participant
May 15, 2023

Figured it out if anyone else runs into this issue:

 

property("Position") returns a _PropertyClasses

_PropertyClasses is an alias for: Property | PropertyGroup | MaskPropertyGroup

 

But setValue() is only valid for Property. It does not exist for PropertyGroup or MaskPropertyGroup

 

For valid TypeScript, you have to first confirm that property("Position") is specifically a Property before attempting to call setValue(). There are many ways to accomplish this (https://www.typescriptlang.org/docs/handbook/2/narrowing.html), but here was my solution:

 

 

var textPos = newTextLayer.property("Position") as Property;
textPos.setValue([0,0]);

 

 

Dan Ebberts
Community Expert
Community Expert
May 15, 2023

Sorry, I totally missed the TypeScript reference. Glad you got it sorted!

Dan Ebberts
Community Expert
Community Expert
May 13, 2023

I think you just need to change the first line:

var newTextLayer = app.project.activeItem.layers.addText("Hello, world.");
newTextLayer.property("Position").setValue([0,0]);
Known Participant
May 13, 2023

That's how I had it initially when I first got the error. I was hoping specifically setting the variable type to TextLayer might address the issue, but it didn't.

Dan Ebberts
Community Expert
Community Expert
May 14, 2023

It works fine for me. I've never seen that error message--I'm not sure how you would get that.