import { fail , isPrimitive , createScalarNode , ISimpleType , TypeFlags , IValidationContext , IValidationResult , typeCheckSuccess , typeCheckFailure , isType , Primitives , AnyObjectNode , SimpleType , devMode } from "../../internal" ; import { assertArg } from "../../utils" ; internal hidden export class Literal < T > extends SimpleType < T , T , T > { readonly value : T ; readonly flags = TypeFlags . Literal ; constructor ( value ) { super ( JSON . stringify ( value ) ) ; this . value = value ; } instantiate ( parent , subpath , environment , initialValue ) { return createScalarNode ( this , parent , subpath , environment , initialValue ) ; } describe ( ) { return JSON . stringify ( this . value ) ; } isValidSnapshot ( value , context ) { if ( isPrimitive ( value ) && value === this . value ) { return typeCheckSuccess ( ) ; } return typeCheckFailure ( context , value , `Value is not a literal ${ JSON . stringify ( this . value ) }` ) ; } } param value returns export function literal < S extends Primitives > ( value ) { assertArg ( value , isPrimitive , "primitive" , 1 ) ; return new Literal < S > ( value ) ; } param type returns export function isLiteralType < IT extends ISimpleType < any > > ( type ) { return isType ( type ) && ( type . flags & TypeFlags . Literal ) > 0 ; }