Skip to content

Fix #19666: Check for symbol types in template expressions - #22251

Merged
Mohamed Hegazy (mhegazy) merged 3 commits into
microsoft:masterfrom
jack-williams:jack-williams/no-implicit-symbol-to-string
Mar 30, 2018
Merged

Mohamed Hegazy (mhegazy) merged 3 commits into
microsoft:masterfrom
jack-williams:jack-williams/no-implicit-symbol-to-string

Conversation

@jack-williams

Copy link
Copy Markdown
Collaborator

Fixes #19666

Check for symbol types in template expressions:

let symbol!: symbol;
const templateStr = `hello ${symbol}`;
// error TS2352: Type 'symbol' cannot be converted to type 'string'.

The following cases were already handled (but have been added to the tests):

let symbol!: symbol;
let str = "hello ";
const appendStr = "hello " + symbol;
str += symbol;

This PR does not deal with tagged templates.

Question: Should the type of the checked expression be any when encountering an error?

Mohamed Hegazy (@mhegazy)

@@ -0,0 +1,8 @@
// Fix #19666

let symbol!: symbol;

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'm not huge on the test file name here, but it's fine. Can you add a case where you have a value of type symbol | number (or something in a union)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Will do. I'm open to changing the name. Perhaps templateStringWithSymbolExpressionErrors.ts ?

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Mar 6, 2018 •

Copy link
Copy Markdown
Member

I think you should just leave the type of the string literal to be a string; the type of the expression itself isn't observed otherwise. The only time we are concerned with the expression type is a tagged template.

Which reminds me! Can you add a test with the following contents? 😄

// taggedTemplateStringWithSymbolExpression01.ts

declare function foo(template: any, val: symbol): number;
let x!: symbol;

let result: number = foo `${x}`;

Ideally, the test output won't have any errors.

@jack-williams

Copy link
Copy Markdown
Collaborator Author

Update: Daniel Rosenwasser (@DanielRosenwasser)

  • Add union types to test with errors.
  • Add test case for tagged template with symbol param and number return type (no errors).

To consider:
Rename test noImplicitSymbolToString.ts ==> templateStringWithSymbolExpressionErrors.ts

@jack-williams

Copy link
Copy Markdown
Collaborator Author

ping Daniel Rosenwasser (@DanielRosenwasser) (just incase this got lost under a pile somewhere)

@mhegazy
Mohamed Hegazy (mhegazy) merged commit 31c3ef5 into microsoft:master Mar 30, 2018
@jack-williams
Jack Williams (jack-williams) deleted the jack-williams/no-implicit-symbol-to-string branch April 3, 2018 08:37
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants