15

In a react-native application, I have the style

const styles = StyleSheet.create({
    text: {
        textAlign: "center"
    },
})

used in <Text style={styles.text} />, but the tsc compiler gives the following error:

Types of property 'textAlign' are incompatible.
Type 'string' is not assignable to type '"center" | "auto" | "left" | "right"'.

The definition of TextStyle in @types/react-native includes:

export interface TextStyle extends TextStyleIOS, TextStyleAndroid, 
ViewStyle {
    // ...
    textAlign?: "auto" | "left" | "right" | "center"
    // ...
}

Why is the compiler complaining about incompatibility? It seems that it is inferring the type of textAlign to be the too general string instead of checking the actual value ("center").

I know that I can use a as TextStyle to avoid the problem, but I would like to know why it is happening and if I should file a ticket against the compiler.

4
  • or maybe the typings mantainer, as it seems the expected values is not a string... Commented Mar 30, 2017 at 15:10
  • I think the typings maintainer is correct in limiting the values, but the compiler should carry the actual value through to type comparison instead of inferring string. Otherwise, what is the purpose of having string literal types? Commented Mar 30, 2017 at 15:14
  • You're correct indeed, it seems a tsc error then.. :/ Commented Mar 30, 2017 at 17:10
  • Finding the same issue, I used to have code like this that worked, which is now broken - think it's tsc issue Commented Mar 15, 2018 at 10:21

4 Answers 4

27

If you're using Typescript 3.4+ you can make use of the as const notation:

export const locationInput = {
  textAlign: 'center' as const
};

For more info check documentations here.

Sign up to request clarification or add additional context in comments.

Comments

24

This ought to work:

const styles = StyleSheet.create({
    text: {
        textAlign: "center" as "center"
    },
})

Normally TypeScript would type textAlign as string, but since it can't just be any string, you can cast it to the more specific type.

1 Comment

I can also use text: { ... } as TextStyle, but I still think the compiler should handle that.
6

Another option whould be:

import { TextAlignProperty } from 'csstype'

const styles = StyleSheet.create({
    text: {
        textAlign: "center" as TextAlignProperty
    },
})

1 Comment

Thank you for this solution, it really is exactly what I needed! :)
3

I wanted to do similiar in React TypeScript and the answers here are close but were not quite the working solution. Instead I found I had to import Property from csstype, and then cast as Property.TextAlign.

In the below Example I call to a function that returns Property.TextAlign instead of a regular string.

import { Property } from 'csstype'

...

const getTextAlign = (dataType: string):Property.TextAlign => {
    var output:string = "left";

    if(dataType === "Money"){
        output = "right";
    }

    return output as Property.TextAlign;
}

...

<td style={{textAlign: getTextAlign(this.state.dataType) }}>

Trying to follow the other solutions, I just got an error that TextAlignProperty wasn't defined in csstype. Here I was particularly working in a PCF control, but that shouldn't matter. This may be a difference due to version of React.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.