-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why does the ts type checker return structurally equivalent types as the first one requested? #480
Comments
Here's a compiler api example: import * as ts from "typescript";
const testFilePath = "/file.ts";
const testFileText = `
export type AvatarSize = 'sm' | 'md' | 'lg' | 'xl';
export type CardAvatarSize = 'sm' | 'md' | 'lg' | 'xl';
let v1: AvatarSize, v2: CardAvatarSize;
`;
// common setup
const testSourceFile = ts.createSourceFile(testFilePath, testFileText, ts.ScriptTarget.Latest);
const variableStatement = testSourceFile.statements.find(ts.isVariableStatement)!;
const variableDeclarations = variableStatement.declarationList.declarations;
// outputs: "AvatarSize", "AvatarSize"
const typeChecker1 = getTypeChecker();
logTypeText(typeChecker1, variableDeclarations[0]);
logTypeText(typeChecker1, variableDeclarations[1]);
// outputs: "CardAvatarSize", "CardAvatarSize"
const typeChecker2 = getTypeChecker();
logTypeText(typeChecker2, variableDeclarations[1]);
logTypeText(typeChecker2, variableDeclarations[0]);
function logTypeText(typeChecker: ts.TypeChecker, declaration: ts.VariableDeclaration) {
const type = typeChecker.getTypeAtLocation(declaration.type!);
console.log(typeChecker.typeToString(type));
}
function getTypeChecker() {
const options: ts.CompilerOptions = { target: ts.ScriptTarget.ES5 };
const host: ts.CompilerHost = {
fileExists: filePath => filePath === testFilePath,
directoryExists: dirPath => dirPath === "/",
getCurrentDirectory: () => "/",
getDirectories: () => [],
getCanonicalFileName: fileName => fileName,
getNewLine: () => "\n",
getDefaultLibFileName: () => "",
getSourceFile: filePath => filePath === testFilePath ? testSourceFile : undefined,
readFile: filePath => filePath === testFilePath ? testFileText : undefined,
useCaseSensitiveFileNames: () => true,
writeFile: () => {}
};
return ts.createProgram({
options,
rootNames: [testFilePath],
host
}).getTypeChecker();
} |
I think this is by design. For example, when changing it to this: export type AvatarSize = string;
export type CardAvatarSize = string; It will always output |
Note the following behaviour of the compiler: // test.ts
export type AvatarSize = 'sm' | 'md' | 'lg' | 'xl';
export type CardAvatarSize = 'sm' | 'md' | 'lg' | 'xl';
export class Test {
method1() {
return "sm" as AvatarSize;
}
method2() {
return "sm" as CardAvatarSize;
}
} Outputs the following declaration file: // test.d.ts
export declare type AvatarSize = 'sm' | 'md' | 'lg' | 'xl';
export declare type CardAvatarSize = 'sm' | 'md' | 'lg' | 'xl';
export declare class Test {
method1(): AvatarSize;
method2(): AvatarSize;
} I'm going to reopen this issue for now. |
Response from the compiler team in microsoft/TypeScript#28197:
|
From #450. For example:
I bet this is by design, but logging it anyway.
The text was updated successfully, but these errors were encountered: