CashScript / cashscript

⚖️ Easily write and interact with Bitcoin Cash smart contracts
https://cashscript.org
MIT License
111 stars 77 forks source link

added generics for contract type-safety #192

Open PHCitizen opened 1 month ago

PHCitizen commented 1 month ago

this is the implementation of the snippets I post on telegram. Types are a bit different because I make a fallback if "any" is passed. this is needed if the user are using javascript or artifact are imported somewhere, and we cant infer the types

latest ts playground

btw i remove infer using string. i think if they will use it, much better to use interface and define type there.

vercel[bot] commented 1 month ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
cashscript ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 7, 2024 5:14am
rkalis commented 1 month ago

Hey, this is really cool! Thanks for creating this PR. This functionality would be really nice to have, we saw that there's some TODOs / refactors still needed. If we want to integrate this, then the inferring would also need to work for the unlock parts of the contract for sure.

We will have more bandwidth to get this implemented / integrated once the debugging tooling upgrade (v0.10) is released, so we'll get back to this issue after that.

PHCitizen commented 1 month ago

on commit 90191fb i use ignore that error, because we know that it must be indexed and we are setting the right value if we don't use ignore, we need to cast type manually

const name = f.name as keyof TFunctions;
this.functions[name] = this.createFunction(f) as TFunctions[keyof TFunctions];