Within my Django project, there is a template file that I have created:
The content of my_template.html:
<script>
let configuration = '{{ my_config_variable }}';
</script>
<script src="{% static 'script.js' %}"></script>
Content within script.ts:
// Utilizing the config variable:
console.log(configuration);
This process functions smoothly in JavaScript. However, when attempting to compile this in TypeScript, errors are generated.
How can variables be efficiently passed from Django to TypeScript while still maintaining type safety?
Edit:
I made some adjustments as seen below and it is functional, although uncertainty remains if this is the correct approach.
Updates made to my_template.html:
<script>
let configuration = '{{ my_config_variable }}';
main(configuration);
</script>
<script src="{% static 'script.js' %}"></script>
Script.ts now includes:
function main(config) {
// Working with the config variable:
console.log(config);
}