Skip to content

Omit tostring from template literal spans if they are strings#948

Merged
Perryvw merged 3 commits intoTypeScriptToLua:masterfrom
hazzard993:tostring-enhancement
Dec 21, 2020
Merged

Omit tostring from template literal spans if they are strings#948
Perryvw merged 3 commits intoTypeScriptToLua:masterfrom
hazzard993:tostring-enhancement

Conversation

@hazzard993
Copy link
Copy Markdown
Contributor

Closes #940

Seems like a straight forward fix

Using the existing isStringType to determine if the type is a string

["string | any", true],
])("template span expect tostring for type (%p) - %p", (type, expectToString) => {
util.testFunction`
function func(msg: ${type}) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be simplified to something like this?

let msg: ${type} ;
// @ts-ignore
return  \`\${msg}\`;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhat, TypeScript is a bit too smart for that case

let msg: string | number = 5;
msg; // type: number

But this gets around it

let msg = 5 as string | number;
msg; // type: string | number

// @ts-ignore
return func("");
`
.tap(builder => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer one test case for contains "tostring" and one test case for does not contain "tostring". That will result in a small amount of code duplication, but is a lot easier to read imo.

@Perryvw Perryvw merged commit 5ced4c7 into TypeScriptToLua:master Dec 21, 2020
@hazzard993 hazzard993 deleted the tostring-enhancement branch December 21, 2020 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unnecessary tostring in string templates

3 participants