Skip to content

Commit 8e91691

Browse files
Try to infer type
1 parent a0bc324 commit 8e91691

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

packages/firestore/lite-types/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18-
export declare class B1 {}
18+
export declare class B1 {
19+
T;
20+
data?: string;
21+
}
1922
export {};

packages/firestore/lite/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
class A1<T> {
1919
// Non-exported class I want to hide
2020
data?: T;
21+
foo(data: T): T | void {}
2122
}
2223

2324
export class B1 extends A1<string> {}

packages/firestore/scripts/prune-dts.ts

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function main(inputLocation: string, outputLocation: string): void {
4444
>(sourceFile, [dropPrivateApiTransformer.bind(null, typeChecker)]);
4545
const transformedSourceFile: ts.SourceFile = result.transformed[0];
4646
const content = printer.printFile(transformedSourceFile);
47+
console.log(content);
4748
fs.writeFileSync(outputLocation, content);
4849
}
4950

@@ -92,6 +93,16 @@ function maybeHideConstructor(
9293
}
9394
}
9495

96+
function visit<T extends ts.Node>(
97+
typeChecker: ts.TypeChecker,
98+
originalLocation: ts.Node,
99+
d: T
100+
): T {
101+
return ts.transform(d, [
102+
updateInheritedTypeTransformer.bind(null, typeChecker, originalLocation)
103+
]).transformed[0] as T;
104+
}
105+
95106
/**
96107
* Examines `extends` and `implements` clauses and removes or replaces them if
97108
* they refer to a non-exported type. When an export is removed, all members
@@ -148,21 +159,32 @@ function prunePrivateImports<
148159
// Iterate all members of the private type and add them to the
149160
// public type if they are not already part of the public type.
150161
const privateType = typeChecker.getTypeAtLocation(type);
151-
const symbolType = typeChecker.getTypeOfSymbolAtLocation(
152-
privateType.getProperties()[0],
153-
type
154-
);
155-
if (symbolType?.symbol?.members) {
162+
if (privateType?.symbol?.members) {
156163
privateType.symbol.members!.forEach((definition, memberName) => {
157164
if (!currentMembers || !currentMembers.has(memberName)) {
158-
additionalMembers.push(...definition.declarations);
165+
additionalMembers.push(
166+
...definition.declarations.map(d =>
167+
visit(typeChecker, type, d)
168+
)
169+
);
159170
}
160171
});
161172
}
162173
}
163174
}
164175
}
165176

177+
// for (const property of privateType.getProperties()) {
178+
// for (const declaration of property.declarations) {
179+
// if (ts.isPropertyDeclaration(declaration)) {
180+
//
181+
// const localType = typeChecker.typeToTypeNode(typeChecker.getTypeOfSymbolAtLocation(property, type), node, undefined);
182+
//
183+
// const updatedElement = ts.updateProperty(declaration, declaration.decorators, declaration.modifiers, declaration.name, declaration.questionToken || declaration.exclamationToken, localType, declaration.initializer);
184+
// additionalMembers.push(updatedElement);
185+
// }
186+
// }
187+
// }
166188
if (exportedTypes.length > 0) {
167189
prunedHeritageClauses.push(
168190
ts.updateHeritageClause(heritageClause, exportedTypes)
@@ -257,6 +279,44 @@ function extractPublicSymbol(
257279
return publicSymbolsForLocalType[0];
258280
}
259281

282+
const updateInheritedTypeTransformer = (
283+
typeChecker: ts.TypeChecker,
284+
originalLocation: ts.Node,
285+
context: ts.TransformationContext
286+
) => {
287+
return (sourceFile: ts.Node) => {
288+
function visit(node: ts.Node): ts.Node {
289+
if (ts.isTypeReferenceNode(node)) {
290+
const symbol = typeChecker.getSymbolAtLocation(node.typeName);
291+
const replacement = typeChecker.getTypeOfSymbolAtLocation(
292+
symbol!,
293+
originalLocation
294+
);
295+
if (replacement) {
296+
return ts.updateTypeReferenceNode(
297+
node,
298+
ts.createIdentifier('string'),
299+
node.typeArguments
300+
);
301+
} else {
302+
return node;
303+
}
304+
}
305+
306+
return node;
307+
}
308+
309+
function visitNodeAndChildren<T extends ts.Node>(node: T): T {
310+
return ts.visitEachChild(
311+
visit(node),
312+
childNode => visitNodeAndChildren(childNode),
313+
context
314+
) as T;
315+
}
316+
return visitNodeAndChildren(sourceFile);
317+
};
318+
};
319+
260320
const dropPrivateApiTransformer = (
261321
typeChecker: ts.TypeChecker,
262322
context: ts.TransformationContext

0 commit comments

Comments
 (0)