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

Type casting seems impossible in a React TSX file #6897

Closed
jumpinjackie opened this issue Feb 4, 2016 · 3 comments
Closed

Type casting seems impossible in a React TSX file #6897

jumpinjackie opened this issue Feb 4, 2016 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@jumpinjackie
Copy link

It appears I can't use type casting in React TSX files.

Say I have this expression in some method of the React TSX component:

var editTitle = <EditableField>this.refs['editTitle'];

This line is supposed to be: fetch the child component ref'd as "editTitle" and cast it to the type EditableField, so that I can then access methods of EditableField in a strongly-typed manner.

But the typescript compiler appears to be treat that cast expression as a React element creation expression instead, being inside a TSX file and all.

Is it even possible to cast objects inside a React TSX component?

@jumpinjackie
Copy link
Author

Ok, it appears I can use the as operator to do this. If direct casting via <> is not possible, then feel free to close.

@DanielRosenwasser
Copy link
Member

Yup, we introduced the as operator for this. It's usable in both .ts and .tsx files.

@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Feb 4, 2016
@beclamide
Copy link

For completeness in case anyone stumbles across this page. This is the correct way to inline cast in TSX.

(myObject as ObjectType).myProperty = 'foo';

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants