[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);Extends
Section titled “Extends”BaseValue
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
Parameters extends unknown[] | never[] |
ReturnType | unknown |
Properties
Section titled “Properties”| Property | Modifier | Type | Description | Overrides | Inherited from |
|---|---|---|---|---|---|
entityKind | readonly | "Value" | - | - | BaseValue.entityKind |
implementation | public | (ctx, …args) => ReturnType | The 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? | public | string | The function’s name as declared in the TypeSpec source, if any. | - | - |
namespace? | public | Namespace | The namespace in which this function was declared, if any. | - | - |
node? | public | FunctionDeclarationStatementNode | - | - | - |
parameters | public | MixedFunctionParameter[] | The parameters of the function. | - | - |
returnType | public | MixedParameterConstraint | The return type constraint of the function. | - | - |
type | public | Type | Represent 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: string | int32 = b; // Type here would be string | int32` |
valueKind | public | "Function" | - | BaseValue.valueKind | - |