Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export function extractBindingTypes(assignment: HtmlNodeAttrAssignment, context:
typeB = directiveType;
}

// Handle `nothing` and `noChange` symbols
// Since it's not possible to check the details of a symbol due to various restrictions (it's treated as a `unique symbol` or `Symbol()`), all symbols are excluded.
typeB = excludeSymbolsFromUnion(typeB);

// Cache the result
const result = { typeA, typeB };
cache.set(assignment, result);
Expand Down Expand Up @@ -76,6 +80,17 @@ export function inferTypeFromAssignment(assignment: HtmlNodeAttrAssignment, chec
}
}

function excludeSymbolsFromUnion(type: SimpleType): SimpleType {
if (type.kind !== "UNION") {
return type;
}

return {
...type,
types: type.types.filter(t => t.kind !== "ES_SYMBOL" && t.kind !== "ES_SYMBOL_UNIQUE")
};
}

/**
* Relax the type so that for example "string literal" become "string" and "function" become "any"
* This is used for javascript files to provide type checking with Typescript type inferring
Expand Down
3 changes: 1 addition & 2 deletions packages/lit-analyzer/src/test/helpers/compile-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ export function compileFiles(inputFiles: TestFile[] | TestFile = []): { program:

const program = ts.createProgram({
//rootNames: [...files.map(file => file.fileName!), "node_modules/typescript/lib/lib.dom.d.ts"],
rootNames: [...files.map(file => file.fileName!), ...(includeLib ? ["node_modules/typescript/lib/lib.dom.d.ts"] : [])],
//rootNames: files.map(file => file.fileName!),
rootNames: [...files.map(file => file.fileName!), ...(includeLib ? ["node_modules/typescript/lib/lib.dom.d.ts"] : [])], //rootNames: files.map(file => file.fileName!),
options: compilerOptions,
host: compilerHost
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,34 @@ tsTest("Attribute binding: the target attribute is correctly type checked when g

hasNoDiagnostics(t, diagnostics);
});

tsTest("Attribute binding: any symbols are ignored on type checking", t => {
const { diagnostics } = getDiagnostics(`
declare const value: boolean | unique symbol;
html\`<div aria-expanded=\${userInput}></div>\`
`);

hasNoDiagnostics(t, diagnostics);
});

tsTest("Attribute binding: symbols are not treated as any type", t => {
const { diagnostics } = getDiagnostics(`
declare const value: "invalid" | unique symbol;
html\`<div aria-expanded=\${value}></div>\`
`);

hasDiagnostic(t, diagnostics, "no-incompatible-type-binding");
});

tsTest("Attribute binding: lit's nothing is ignored on type checking when returned by a function", t => {
const { diagnostics } = getDiagnostics(`
declare const nothing: unique symbol;
function customIfDef<T>(value: T | null | undefined): T | typeof nothing {
return value ?? nothing;
}
declare const value: boolean | null;
html\`<div aria-expanded=\${customIfDef(value)}></div>\`
`);

hasNoDiagnostics(t, diagnostics);
});
Loading