#CanPassToJavaScript Source

class CanPassToJavaScript (a :: Type) 

This class is used to assert that values of a type can be passed to JavaScript code directly (without conversion) and understood by that code. Specifically, this class is defined for primitives (strings, numbers, booleans), arrays, and records. This assertion is used in a number of places that pass complex values to JS code to restrict the set of types that can be safely passed.

It is still possible to define instances of this class for other, non-primitive types, but you have to know what you're doing and make sure that JS representation is sane and stable. For example, a common trick is to newtype-wrap known JS enumerations to provide type safety:

module HTMLButton
   ( ButtonType  -- NOTE: not exporting the constructor
   , typeButton, typeSubmit, typeReset
   , ButtonProps, button

newtype ButtonType = ButtonType String
instance toJsButtonType :: CanPassToJavaScript ButtonType
typeButton = ButtonType "button" :: ButtonType
typeSubmit = ButtonType "submit" :: ButtonType
typeReset = ButtonType "reset" :: ButtonType

type ButtonProps =
  { type :: ButtonType
  , ...

foreign import button :: ButtonProps -> ReactElement


#readForeign Source

readForeign :: forall a. CanReceiveFromJavaScript a => Foreign -> Maybe a

Verifies if the given raw JS value is of the right type/shape to be represented as a, and if so, coerces the value to a.

#argumentsToArray_ Source

argumentsToArray_ :: Arguments -> Array Foreign

Creates a new Array from an Arguments object.

#getArgument Source

getArgument :: Arguments -> Int -> Maybe Foreign

Gets the value at a specified index of an Arguments object. Returns Nothing if there are not enough arguments.

#mkVarArgEff_ Source

mkVarArgEff_ :: (Arguments -> Effect Unit) -> Foreign

Creates a JS function that takes a variable number of args (via arguments) and calls the provided effectful continuation, passing the arguments as an array.

#CanReceiveFromJavaScriptRecord Source

class CanReceiveFromJavaScriptRecord rowList  where

This class is implementation of isForeignOfCorrectType for records. It validates a given JS hash (aka "object") against a given type row that represents a PureScript record, recursively calling isForeignOfCorrectType for each field.



#CanPassToJavaScriptRecord Source

class CanPassToJavaScriptRecord (rowList :: RowList Type) 

This class is implementation of CanPassToJavaScript for records. It simply iterates over all fields, checking that every field is of a type that also has an instance of CanPassToJavaScript.


Re-exports from Foreign

#Foreign Source

data Foreign :: Type

A type for foreign data.

Foreign data is data from any external unknown or unreliable source, for which it cannot be guaranteed that the runtime representation conforms to that of any particular type.

Suitable applications of Foreign are

  • To represent responses from web services
  • To integrate with external JavaScript libraries.