Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow only value types in string interpolations #44

Closed
zakjan opened this issue Jun 24, 2016 · 4 comments
Closed

Allow only value types in string interpolations #44

zakjan opened this issue Jun 24, 2016 · 4 comments

Comments

@zakjan
Copy link

zakjan commented Jun 24, 2016

export class PageViewModel {
    item: {name: string};
}

<template>
    <!-- fail -->
    ${item}
    <div title="Item: ${item}"></div>
    <!-- ok -->
    ${item.name}
    <div title="Item: ${item.name}"></div>
</template>

It could prevent unintentional implicit casting, for example to "[object Object]".

@zakjan zakjan mentioned this issue Jun 24, 2016
18 tasks
@MeirionHughes MeirionHughes added this to the 0.8 milestone Jun 24, 2016
@MeirionHughes MeirionHughes self-assigned this Jun 24, 2016
@MeirionHughes MeirionHughes changed the title Allow only strings (and maybe numbers) in string interpolations Allow only value types in string interpolations Jun 25, 2016
@MeirionHughes
Copy link
Contributor

MeirionHughes commented Jun 25, 2016

if the class definition has a toString() then that should be okay too.

Might need to add in some functionality in the reflection to traverse through the extended classes to see if toString() is overridden in the class graph.

@zakjan
Copy link
Author

zakjan commented Jun 27, 2016

The default JS behavior for template strings is to always call ToString(result of evaluating expression) https://tc39.github.io/ecma262/#sec-template-literals-runtime-semantics-evaluation Implementing a logic that would allow only non-native toString could be confusing.

TypeScript itself doesn't want different rules of template strings and + operator. See microsoft/TypeScript#7989 and linked issues

I can see two possible solutions. 1) implement an optional strict rule that would allow strings only, everything else would need to be explicitly casted with .toString() 2) follow how TypeScript currently does this, i.e. don't implement this rule at all

@MeirionHughes
Copy link
Contributor

MeirionHughes commented Jun 28, 2016

I think I'm leaning to the later; technically its is not syntactically incorrect to do this. So long as the access expression to the variable is correct and assignment is okay. Then I think anything else is currently out-of-scope.

@MeirionHughes MeirionHughes removed this from the 0.8 milestone Jul 1, 2016
@MeirionHughes MeirionHughes removed their assignment Jul 1, 2016
@MeirionHughes
Copy link
Contributor

MeirionHughes commented Jul 1, 2016

I think if anyone actually did print off an object like above then they will clearly see the result and given it is technically not-incorrect to do so, I won't be spending time on it yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants