You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How can I identify the 2nd element type of [Color, Color?] is optional?
My current attempts have predominantly been focused on the Type<ts.TupleType>, the tuple's underlying type.compilerType, and the tuple elements from type.getTupleElements(), however, I've been unsuccessful.
Any help in finding the information would be greatly appreciated, and thank you for such a fantastic library. 🙏
The text was updated successfully, but these errors were encountered:
I couldn't find a way either using onlyts-morph and had to read into the underlyingcompilerType to achieve it:
functioncheckOptionalTupleElements(type: Type<ts.Type>){conststringifiedTupleIndexes=type.getTupleElements().map((_,index)=>`${index}`)constoptionalIndexed=type.compilerType.getProperties()// `getProperties` will return properties such as `length`, we need remove properties that are not an tuple element index..filter((property)=>stringifiedTupleIndexes.includes(property.escapedNameasstring)).map((typeSymbol)=>(typeSymbol.getFlags()&SymbolFlags.Optional)!==0)returnoptionalIndexed}// Create a new projectconstproject=newProject({useInMemoryFileSystem: true})constsourceFile=project.createSourceFile("test.ts",` type Tuple = [number, number?]; `)consttypeAlias=sourceFile.getTypeAliasOrThrow("Tuple")consttype=typeAlias.getType()constresult=checkOptionalTupleElements(type)console.log(result)// Output: [false, true]
I'm traversing an object type, and I've hit a bit of a road-bump whilst attempting to determine if a tuple's element type is optional.
For example, with a declaration like this:
How can I identify the 2nd element type of
[Color, Color?]
is optional?My current attempts have predominantly been focused on the
Type<ts.TupleType>
, the tuple's underlyingtype.compilerType
, and the tuple elements fromtype.getTupleElements()
, however, I've been unsuccessful.Any help in finding the information would be greatly appreciated, and thank you for such a fantastic library. 🙏
The text was updated successfully, but these errors were encountered: