I'm using this code with Svelte 3:
REPL: https://svelte.dev/repl/bf73fffc1b9442dfbcd492eaa9c048e1?version=3.35.0
<script lang="ts">
const players = {
men: {
john: "high",
bob: "low",
},
};
// const player = "bob"
$: player = "bob";
const test = players.men[player];
console.log(test); //prints undefined
</script>
{player} //bob
{test} //undefined
Typescript even tells me this:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ john: string; bob: string; }'.
No index signature with a parameter of type 'string' was found on type '{ john: string; bob: string; }'.ts(7053)
If I use const player = "bob"
it works!
Why?