Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

typescript not throwing error when passed an object that should throw

$
0
0

I have some code that I expect to throw an error that isn't in typescript and I'm not sure why. I have the following code in a react native project:

  const styles = {    flexGrow: 1,    width: '100%',  } as const  return <View style={styles} />

This is all well and good. However, if I add an invalid style to styles:

  const styles = {    flexGrow: 1,    width: '100%',    invalidStyle: 'bla'  } as const  return <View style={styles} />

Typescript does not throw an error.

This works (throws an error) if I put the styles directly on the style prop on View:

  return <View style={{    flexGrow: 1,    width: '100%',    invalidStyle: 'bla'  }} />

I correctly get Object literal may only specify known properties, and 'randomProps' does not exist in type 'ViewStyle | RecursiveArray<Falsy | ViewStyle | RegisteredStyle<ViewStyle>>'.

Is there anything I can do to make sure I get this error in the non-direct assignment of styles?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>