Skip to content

[I] FunctionValue

A function (fn) declared in the TypeSpec program.

By default, function values have very restrictive types, where the parameters have type never, and the return type is unknown.

To call a function, you must assert the type of the parameters. For example, if you have a FunctionValue that represents the following TypeSpec function:

extern fn example(a: valueof string, b: valueof int32): valueof boolean;

You can assert the parameter types in TypeScript like this:

const exampleFn: FunctionValue = ...; // however you obtain a reference to the function value, it will be strictly typed.
const assertedExampleFn = exampleFn as FunctionValue<[a: string, b: number], boolean>;
// Now you can call assertedExampleFn with the correct types:
ctx.callFunction(assertedExampleFn.implementation, "hello", 10);
  • BaseValue
Type ParameterDefault type
Parameters extends unknown[]never[]
ReturnTypeunknown
PropertyModifierTypeDescriptionOverridesInherited from
entityKindreadonly"Value"--BaseValue.entityKind
implementationpublic(ctx, …args) => ReturnTypeThe JavaScript implementation of the function. WARNING: Calling the implementation function directly is dangerous. It assumes that you have marshaled the arguments to JS values correctly and that you will handle the return value appropriately. Constructing the correct context is your responsibility (use the call methods of FunctionContext or DecoratorContext to create the context for you).--
name?publicstringThe function’s name as declared in the TypeSpec source, if any.--
namespace?publicNamespaceThe namespace in which this function was declared, if any.--
node?publicFunctionDeclarationStatementNode---
parameterspublicMixedFunctionParameter[]The parameters of the function.--
returnTypepublicMixedParameterConstraintThe return type constraint of the function.--
typepublicTypeRepresent the storage type of a value. Example `const a = “hello”; // Type here would be “hello” const b: string = a; // Type here would be string const c: stringint32 = b; // Type here would be stringint32`
valueKindpublic"Function"-BaseValue.valueKind-