1"use strict";(self.webpackChunkfluid_framework_website=self.webpackChunkfluid_framework_website||[]).push([[1177],{620551:(e,n,r)=>{r.r(n),r.d(n,{assets:()=>o,contentTitle:()=>d,default:()=>h,frontMatter:()=>a,metadata:()=>t,toc:()=>c});const t=JSON.parse('{"id":"api/fluid-framework/withtype-interface","title":"WithType Interface","description":"Adds a type symbol to a type for stronger typing.","source":"@site/docs/api/fluid-framework/withtype-interface.md","sourceDirName":"api/fluid-framework","slug":"/api/fluid-framework/withtype-interface","permalink":"/docs/api/fluid-framework/withtype-interface","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{"title":"WithType Interface","sidebar_label":"WithType","displayed_sidebar":"docsSidebar"},"sidebar":"docsSidebar","previous":{"title":"VoidTransactionCallbackStatusBeta","permalink":"/docs/api/fluid-framework/voidtransactioncallbackstatusbeta-typealias"},"next":{"title":"WithValue","permalink":"/docs/api/fluid-framework/withvalue-interface"}}');var s=r(886070),i=r(403793);const a={title:"WithType Interface",sidebar_label:"WithType",displayed_sidebar:"docsSidebar"},d=void 0,o={},c=[{value:"Type Parameters",id:"type-parameters",level:3},{value:"Properties",id:"properties",level:2},{value:"Property Details",id:"property-details",level:2}];function l(e){const n={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",h4:"h4",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",...(0,i.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.p,{children:"Adds a type symbol to a type for stronger typing."}),"\n",(0,s.jsx)(n.admonition,{title:"Sealed",type:"info",children:(0,s.jsx)(n.p,{children:'This type is "sealed," meaning that code outside of the library defining it should not implement or extend it. Future versions of this type may add members or make typing of readonly members more specific.'})}),"\n",(0,s.jsx)(n.h2,{id:"withtype-signature",children:"Signature"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-typescript",children:"/** @sealed */\nexport interface WithType<out TName extends string = string, out TKind extends NodeKind = NodeKind, out TInfo = unknown>\n"})}),"\n",(0,s.jsx)(n.h3,{id:"type-parameters",children:"Type Parameters"}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,s.jsxs)(n.table,{children:[(0,s.jsx)(n.thead,{children:(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.th,{children:"Parameter"}),(0,s.jsx)(n.th,{children:"Constraint"}),(0,s.jsx)(n.th,{children:"Default"}),(0,s.jsx)(n.th,{children:"Description"})]})}),(0,s.jsxs)(n.tbody,{children:[(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:"TName"}),(0,s.jsx)(n.td,{children:"string"}),(0,s.jsx)(n.td,{children:"string"}),(0,s.jsxs)(n.td,{children:["Same as ",(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/treenodeschema-typealias",children:"TreeNodeSchema"}),'\'s "Name" parameter.']})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:"TKind"}),(0,s.jsx)(n.td,{children:(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/nodekind-enum",children:"NodeKind"})}),(0,s.jsx)(n.td,{children:(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/nodekind-enum",children:"NodeKind"})}),(0,s.jsxs)(n.td,{children:["Same as ",(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/treenodeschema-typealias",children:"TreeNodeSchema"}),'\'s "Kind" parameter.']})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:"TInfo"}),(0,s.jsx)(n.td,{}),(0,s.jsx)(n.td,{children:"unknown"}),(0,s.jsxs)(n.td,{children:["Same as ",(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/treenodeschema-typealias",children:"TreeNodeSchema"}),'\'s "Info" parameter: format depends on the Kind.']})]})]})]}),"\n",(0,s.jsx)(n.h2,{id:"withtype-remarks",children:"Remarks"}),"\n",(0,s.jsxs)(n.p,{children:["Powers ",(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/treenode-class",children:"TreeNode"}),"'s strong typing setup."]}),"\n",(0,s.jsx)(n.h2,{id:"withtype-example",children:"Example"}),"\n",(0,s.jsx)(n.p,{children:"Narrow types for overloading based on NodeKind"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-typescript",children:'function getKeys(node: TreeNode & WithType<string, NodeKind.Array>): number[];\nfunction getKeys(node: TreeNode & WithType<string, NodeKind.Map | NodeKind.Object>): string[];\nfunction getKeys(node: TreeNode): string[] | number[];\nfunction getKeys(node: TreeNode): string[] | number[] {\n\tconst schema = Tree.schema(node);\n\tswitch (schema.kind) {\n\t\tcase NodeKind.Array: {\n\t\t\tconst arrayNode = node as TreeArrayNode;\n\t\t\tconst keys: number[] = [];\n\t\t\tfor (let index = 0; index < arrayNode.length; index++) {\n\t\t\t\tkeys.push(index);\n\t\t\t}\n\t\t\treturn keys;\n\t\t}\n\t\tcase NodeKind.Map:\n\t\t\treturn [...(node as TreeMapNode).keys()];\n\t\tcase NodeKind.Object:\n\t\t\treturn Object.keys(node);\n\t\tdefault:\n\t\t\tthrow new Error("Unsupported Kind");\n\t}\n}\n'})}),"\n",(0,s.jsx)(n.h2,{id:"properties",children:"Properties"}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,s.jsxs)(n.table,{children:[(0,s.jsx)(n.thead,{children:(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.th,{children:"Property"}),(0,s.jsx)(n.th,{children:"Alerts"}),(0,s.jsx)(n.th,{children:"Modifiers"}),(0,s.jsx)(n.th,{children:"Type"}),(0,s.jsx)(n.th,{children:"Description"})]})}),(0,s.jsxs)(n.tbody,{children:[(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/withtype-interface#_typenamesymbol_-property",children:"[typeNameSymbol]"})}),(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"Deprecated"})}),(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"readonly"})}),(0,s.jsx)(n.td,{children:"TName"}),(0,s.jsxs)(n.td,{children:[(0,s.jsx)(n.p,{children:"Type symbol, marking a type in a way to increase type safety via strong type checking."}),(0,s.jsxs)(n.p,{children:["Use ",(0,s.jsx)(n.code,{children:"Tree.schema(theNode)"})," for schema related runtime data access. For type narrowing, use ",(0,s.jsx)(n.code,{children:"WithType"})," instead of the symbols directly."]})]})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/withtype-interface#_typeschemasymbol_-property",children:"[typeSchemaSymbol]"})}),(0,s.jsx)(n.td,{}),(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"readonly"})}),(0,s.jsxs)(n.td,{children:[(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/treenodeschemaclass-typealias",children:"TreeNodeSchemaClass"}),"<TName, TKind, ",(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/treenode-class",children:"TreeNode"}),", never, boolean, TInfo>"]}),(0,s.jsx)(n.td,{children:"Type symbol, marking a type in a way to increase type safety via strong type checking."})]})]})]}),"\n",(0,s.jsx)(n.h2,{id:"property-details",children:"Property Details"}),"\n",(0,s.jsx)(n.h3,{id:"_typenamesymbol_-property",children:"[typeNameSymbol]"}),"\n",(0,s.jsx)(n.p,{children:"Type symbol, marking a type in a way to increase type safety via strong type checking."}),"\n",(0,s.jsxs)(n.p,{children:["Use ",(0,s.jsx)(n.code,{children:"Tree.schema(theNode)"})," for schema related runtime data access. For type narrowing, use ",(0,s.jsx)(n.code,{children:"WithType"})," instead of the symbols directly."]}),"\n",(0,s.jsx)(n.admonition,{title:"This API is deprecated and will be removed in a future release.",type:"warning",children:(0,s.jsxs)(n.p,{children:["Use ",(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/#typesc
1hemasymbol-variable",children:"typeSchemaSymbol"})," instead."]})}),"\n",(0,s.jsx)(n.h4,{id:"_typenamesymbol_-signature",children:"Signature"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-typescript",children:"get [typeNameSymbol](): TName;\n"})}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.strong,{children:"Type"}),": TName"]}),"\n",(0,s.jsx)(n.h4,{id:"_typenamesymbol_-remarks",children:"Remarks"}),"\n",(0,s.jsxs)(n.p,{children:["This should be redundant with ",(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/#typeschemasymbol-variable",children:"typeSchemaSymbol"}),", but is kept for improved compiler performance. If we just rely on ",(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/#typeschemasymbol-variable",children:"typeSchemaSymbol"}),', the compiler has to work much harder to distinguish schema in unions, which can cause large schema unions to hit "error TS2859: Excessive complexity comparing types".']}),"\n",(0,s.jsx)(n.p,{children:"This property is not intended for any use other than helping the compiler distinguish schema types, and should not be referenced in any code other than internal to the tree package."}),"\n",(0,s.jsx)(n.h3,{id:"_typeschemasymbol_-property",children:"[typeSchemaSymbol]"}),"\n",(0,s.jsx)(n.p,{children:"Type symbol, marking a type in a way to increase type safety via strong type checking."}),"\n",(0,s.jsx)(n.h4,{id:"_typeschemasymbol_-signature",children:"Signature"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-typescript",children:"get [typeSchemaSymbol](): TreeNodeSchemaClass<TName, TKind, TreeNode, never, boolean, TInfo>;\n"})}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.strong,{children:"Type"}),": ",(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/treenodeschemaclass-typealias",children:"TreeNodeSchemaClass"}),"<TName, TKind, ",(0,s.jsx)(n.a,{href:"/docs/api/fluid-framework/treenode-class",children:"TreeNode"}),", never, boolean, TInfo>"]})]})}function h(e={}){const{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(l,{...e})}):l(e)}},403793:(e,n,r)=>{r.d(n,{R:()=>a,x:()=>d});var t=r(830758);const s={},i=t.createContext(s);function a(e){const n=t.useContext(i);return t.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function d(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:a(e.components),t.createElement(i.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.