winglang / winglang/wing

Cannot use `cdktf.Lazy.stringValue` in Wing

Open
#7,014 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

🐛 bug 📐 language-design 🛠️ compiler
Dominant language
TypeScript
Stars
5.4k
Forks
215
Avg merge
2h 9m
Merged PRs (30d)
27

Description

I tried this:
bring "cdktf" as cdktf;

let token = cdktf.Lazy.stringValue({
  produce: () => {
    if this.bundleHash == nil {
      throw "bundleHash was not set";
    }
    return this.bundleHash;
  }
});
This happened:

Multiple errors:

Expected type to be "IStringProducer", but got "Json" instead
"preflight (): unknown" is not a legal JSON value
I expected this:

I expected to have some way to create an object that satisfies the IStringProducer in preflight without needing to define a class. It's possible I could define a new preflight class which implements the IStringProducer interface, instantiate the class, and pass it to Lazy.stringValue, but that's not really desirable here since I want an object that's not a construct, and by default any preflight class is a construct.

I feel like the ideal solution would be to allow the Wing code above where an anonymous object is created. Optionally, the user should be able to provide the interface name before the object literal, to write code like this:

let producer = cdktf.IStringProducer {
  produce: () => {
    if this.bundleHash == nil {
      throw "bundleHash was not set";
    }
    return this.bundleHash;
  }
};
let token = cdktf.Lazy.stringValue(producer);
Is there a workaround?

You can create an extern like this:

class Util {
  pub static extern "./function-aws.ts" asStringToken(producer: (): str?): str;
}

With this implementation:

import { Lazy } from "cdktf";

import types from "./util.extern";

export const asStringToken: types["asStringToken"] = (producer) => {
  return Lazy.stringValue({ produce: producer });
};

And call it like this:

Util.asStringToken(() => { return "implementation" });
Anything else?

No response

Wing Version

0.82.5

Node.js Version

20.12.2

Platform(s)

No response

Community Notes
  • Please vote by adding a 👍 reaction to the issue to help us prioritize.
  • If you are interested to work on this issue, please leave a comment.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the Wing compiler path for object literals passed to interface-typed parameters, using the IStringProducer argument in cdktf.Lazy.stringValue as the reproduction. Compare the current Json and illegal JSON errors with the requested anonymous interface object syntax; done means the example type-checks without requiring a preflight class.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.